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?
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
.
try this
RewriteRule ^(.*)index\.php$ /$1 [R=301,NC]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With