Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove index.php url rewriting .htaccess

I'd written this code for 301 redirect

RewriteCond %{THE_REQUEST} ^.*\/index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

It is working well in case if I do visit my site as http://mysite.com/index.php, it redirects me to http://mysite.com

But on my localhost if I try to visit index.php as localhost/mysite/index.php it redirects me to localhost.

How could I solve this problem? Is the code written above is correct?

like image 782
Mohammad Faisal Avatar asked Sep 11 '13 13:09

Mohammad Faisal


2 Answers

It looks like you have your htaccess file in your document root on your server, and in the mysite directory on localhost. Since the location of the htaccess file is pretty important on how it routes URIs, you need to make it indifferent to the location of the file. You can do this by extracting the path info from your condition instead of the URI that's passed into the rule to match against:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

The %{THE_REQUEST} variable is the first line of the actual HTTP request, which looks something like:

GET /path/index.php HTTP/1.1

The pattern first matches any number of possible METHODS (GET, POST, HEAD, etc), then it creates a grouping of the URI path that's before the index.php, then ends the matching, since we don't really care what's after the index.php.

like image 69
Jon Lin Avatar answered Nov 05 '22 12:11

Jon Lin


try this

RewriteRule ^(.*)index\.php$ /$1 [R=301,NC] 
like image 25
ratnesh dwivedi Avatar answered Nov 05 '22 10:11

ratnesh dwivedi