I have following urls:
1.Need to change From:
localhost/laravel/page/about-us/
to
localhost/laravel/about-us/
Laravel's Routing is one of the primary functionalities of the Framework, if you can't use it properly by yourself I suggest you to read the documentation.
I think the first example is explicit enough:
Route::get('/', function()
{
return 'Hello World';
});
This Route catches all of the requests that are sent to the / url (the base url)
So, you should have a look at your \App\Http\routes.php file and replace the line that starts with :
Route::get('page/about-us', function() ...
With:
Route::get('about-us', function() ...
And since you seem to be using xampp or other easyphp friends, you should probably rename your base directory from laravel to laravelproj if you want it to be available from http://localhost/laravelproj instead of http://localhost/laravel.
Defining the default .htaccess file should also help:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
If this doesn't answer your question, I think you need to be more explicit.
Related
This seems so basic, and there's a plethora of questions regarding .htaccess online, but after two days of research, I still can't make mine work the way I want.
What I want is:
Force https on all requests
Always use the "www" version of the url.
Work on multiple domains (but not redirect them all to a master domain). All my domains point to the same folder (so they'd use the same codebase), and in its root is the .htaccess file.
Remove the "index.php" part of the url, to make it human and SEO friendly.
This is what I have so far:
Start with the basic .htaccess code for CodeIgniter, as shown in the userguide:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
Originally, the last line had the [L] flag, but I omitted it, so it will continue to the following rules.
(Am I correct in assuming that it takes the url output in the previous RewriteRule, and perform the following matches on it?)
# for non www urls, add www and force https:
RewriteCond %{HTTP_HOST}(.*) !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R,L]
# for www urls, just force https:
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
The above code is achieving tasks 1-3 of my list above, but the index.php is still showing in the address bar.
How do I remove it?
Well, I (partially) gave up on .htaccess, and solved my problem in a different way:
I'm now using CodeIgniter hook to deal with forcing https, and leave htaccess to deal only with forcing www and removing index.php
So I removed the last line (RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]), and added an ssl hook to my application, and all is working now.
The hook function, in case anyone is interested, is this:
function force_ssl()
{
if ($_SERVER['HTTP_HOST']!='localhost' && $_SERVER['HTTP_HOST']!='10.0.2.2')
{
$CI =& get_instance();
$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
}
}
initally, i'm sorry about my question-title, but really nothing better came into my mind....
i've got a .htaccess file that does the "usual" rewriting:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
this gives me the possibility of have this url for example (i'm working with xampp):
localhost/<project>/en/products/electronic/television/
so this url is in the $_GET['url'] variable and i can use it (as an array) to load the controller in my HMVC pattern.
and, to not have to enter the language ("en" in the example above), i've implemented a function that inserts automatically "en". basically that means the following url gives the same controller, and respectively the same output.
localhost/<project>/products/electronic/television/
but now i wonder, that my javascript files (jquery & co), that are lying in the folder
<project>/lib/js/query/jquery.min.js
can be simply required like this:
<script type="text/javascript" src="/<project>/lib/js/jquery/jquery.js"></script>
i expected that the requested path would eventually be something like this (as i add the language in the $_GET['url'] variable and it would try to load a controller and fails...
src="/<project>/en/lib/js/jquery/jquery.js"
well, in that case i would be glad that it works this way, but it brought me to the idea to try this:
$(document).ready(function() {
alert("this is a test");
$.get("/<project>/<config-dir>/<config-file>", function(data) {
alert(data);
});
});
voila, it outputs the config file.
i think that the behavior will be the same on a productive server, but then just without the "/" nodes in the paths.
how can i prevent this, without moving the config-file from document root?
basically, i've ensured that XSS-attacks won't (almost) be unsuccessful. the "almost" just because there is no 100% safety.
i hope i expressed myself well :)
thanks for the help/suggestions.
well, i found out, what the problem was, why i could access the config file: the rewrite conditions. they say simply, that the following rule should became effective if the requested filename is not a directory or file or link(?).
RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-d not a directory
# RewriteCond %{REQUEST_FILENAME} !-f not a file
# RewriteCond %{REQUEST_FILENAME} !-l not a link(?)
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
so basically this explains, why the rewriting rule did not work on ajax-requests (it did not work on normal requests as well)
now my problem with unallowed access is solved.
to exclude some specific directories from the rewrite rule i added this rule for:
RewriteRule ^(<dirname>)($|/) - [L]
I am new to CodeIgniter. I have an XAMPP server on Windows 8. Everything is fine, but the problem is about my URL, it doesn't look friendly. It looks like localhost/ci/index.php/site/home (where ci is my root folder for CodeIgniter). I want to make the URL more clean, like localhost/ci/home, how can I do it?
My CodeIgniter version is 2.1.2.
I have done some research already, but in most of the cases it says to change the .htaccess file of CodeIgniter. But I have nothing in the .htaccess file; it's empty, except the line "Deny from all".
You can do this, in config/config.php:
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST']; //you can also leave blank this CI tend to find this by himself
$config['index_page'] = '';
And try the following .htaccess. I use it for many sites, and it satisfies me.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
If it is not working yet, use a phpinfo(); and check if mod_rewrite is enabled. If it is not enabled, you can follow the instructions in Stack Overflow question How do you enable mod_rewrite? to enable that.
If it is not working yet and mod_rewrite is enabled yet, you can try to switch these in config/config.php:
$config['uri_protocol'] = 'AUTO'; //If not working, try one of these:
'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
You need to replace "Deny from all" with this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
http://ellislab.com/codeigniter/user-guide/general/urls.html
Looking at the official documentation, CodeIgniter URLs:
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on<br>
RewriteCond $1 !^(index\.php|images|robots\.txt)<br>
RewriteRule ^(.*)$ /index.php/$1 [L]<br>
Also if you are using Apache place .htaccess file in your root web directory. For more information, look in Codeigniter .htaccess.
I'm pretty new to mod-rewrites, and I'm having trouble getting this right:
I'd like http://myurl.com/special and http://www.myurl.com/special to both redirect to http://myurl/index.php/special without changing the url for the visitor in their browser.
currently, my .htaccess looks like this, to remove www from URLs
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.myurl.com [NC]
RewriteRule ^(.*)$ http://myurl.com/$1 [L,R=301]
</IfModule>
I tried a bunch of different things to redirect the special URL, but nothing has worked for me so far, each with a different undesirable result. Please let me know if you have an idea about what I can add here!
I tried this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^special$ /index.php/special$1 [NC]
RewriteCond %{HTTP_HOST} ^www.myurl.com [NC]
RewriteRule ^(.*)$ http://myurl.com/$1 [R=301]
</IfModule>
That makes http://myurl.com/special redirect to the default controller, and makes http://www.myurl.com/special redirect to http://myurl.com/index.php/special AND changes the url in the browser. Neither of those is quite right, although the second is closer to what I need.
oh no you don't need htaccess to do that just use CI's routes. Here first change your HTACESS to this:
# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
This one is way way better and you never have to change it for anything not for img folders or anything just leave it pure. now for your issue.
open routes php in the Application->config folder.
Now in CI all files route through index.php by default so i am assuming you mean your default controller anyway to route "controllers" like in your case special back to the main site just do this:
if your default controller is still "welcome" as it is downloaded you would write a route like this:
$route['special/(:any)'] = "welcome";
your url will not change and everything will be perfect
BUT DONT STOP READING YET, JUST TO MAKE SURE.
this is what your routes would look like after your done:
//these two are for CI
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";
//this is your route sending all and any traffic for special
//no matter how many levels it will always go to welcome->index.
$route['special/(:any)'] = "welcome";
//if you want to use sub-pages or other functions within your welcome controller
//i.e. welcome->signup. then just send the route information to the controller like this:
$route['special/(:any)'] = "welcome/$1";
The routing class in php is very powerful and this is just a piece here for more detailed info on routing see this page:
CodeIgniter User Guide -> URI Routing
Really hope i haven't confused you more. Good Luck
Assuming www.myurl.com and myurl.com are both set-up as ServerAliases in your VirutalHost, then something as simple as...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I am having difficulty with code igniter routing.
http://www.mysite.com goes to the right controller and does the right thing. However, http://www.mysite.com/?ref=p&t2=455 causes a 404 error. Also http://www.mysite.com/mycontroller/mymethod/?ref=p&t2=455 works fine.
I changed the uri_protocol in the config.php file and tried different values. Auto seems to work the best.
My theory is that code igniter is using the query parameters to do the routing. The problem is that these query parameter have nothing to do with routing.
How do I tell code igniter to ignore query parameters for the default controller?
Note, I followed the instructions online to remove the index.php from the URL. I dont think its causing a problem, but here is my .htaccess file just in case:
RewriteEngine On
RewriteBase /~trifecta/prod/
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php?/$1 [L]
With that rewrite rule RewriteRule ^(.*)$ /index.php?/$1, here are some examples of rewrites that are occuring:
http://www.mysite.com =>
http://www.mysite.com/index.php?/ (actually, this might not be being rewritten at all)
http://www.mysite.com/mycontroller/mymethod/?ref=p&t2=455 =>
http://www.mysite.com/index.php?/mycontroller/mymethod/?ref=p&t2=455
http://www.mysite.com/?ref=p&ts=455 =>
http://www.mysite.com/index.php?/?ref=p&t2=455
The first one will work whether it is being rewritten or not. CodeIgniter is processing either an empty query string (which is easy) or a query string of simply "/".
The second one (which also works) is being rewritten, but CodeIgniter is able to process its query string, which is /mycontroller/mymethod/?ref=p&t2=455. CI turns that into an array of segments as
[0] => mycontroller
[1] => mymethod
[2] => ?ref=p&t2=455
Array index 2 ultimately gets ignored by anything you're doing.
The third one (which does not work is being rewritten, and CodeIgniter can't process its query string at all. Its query string is rewritten to: /?ref=p&t2=455. That makes for an array of segments that looks like this:
[0] => ?ref=p&t2=455
which doesn't match any controller on your site.
Probably, you'll fix the whole thing by modifying the RewriteRule from
RewriteRule ^(.*)$ /index.php?/$1 to
RewriteRule ^(.*)$ /index.php/$1
at which point you'd probably want to change the uri_protocol config back to PATH_INFO.
It seems to be a limitation in the way CodeIgniter is designed, not as a result of your routing and/or .htaccess. Someone filed a bug report here. However, instead of using this and adding your mymethod code into your index method of your default controller, you could use the _remap() function like so:
function _remap($method)
{
if ($method == 'index' && count($_GET) > 0)
{
$this->mymethod();
}
else
{
$this->index();
}
}
With the following configuration it works fine for me. http://www.site.com/?x=y gets routed to the index method of the default controller.
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
system/application/config/config.php
$config['base_url'] = "http://www.site.com/";
$config['index_page'] = "";
$config['uri_protocol'] = "PATH_INFO";
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?';
Remember to add ? to the list of permitted characters in the URL. Perhaps this is causing a problem. I just tried this setup with a clean installation of CodeIgniter and it worked fine. These were the only changes I needed to do.
Here is how I got around it:
1. I have a standard rewrite in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
This is likely unrelated but...
in index.php the file outside the application folder I added:
$pattern = "/\?.*$/";
$replacement = '';
$_SERVER['REQUEST_URI'] =preg_replace($pattern, $replacement, $_SERVER['REQUEST_URI']);
this stops codeigniter from trying to use the querystring in the request. Now did you actually need the $_GET variables? They need to be parsed out of $_SERVER['REDIRECT_QUERY_STRING'] which will be set if you used mod_rewrite as above.
Try the CodeIgniter Super .htaccess file