5

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?

1
  • On your localhost, you need to add RewriteBase /mysite/ above those lines since the site isn't at the root of server Commented Sep 11, 2013 at 13:34

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

how do this code works RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ )? a little explanation on elements
still not working on localhost. I'd been redirected to localhost instead of localhost/mysite
Does it capture the URL parameters?
4

try this

RewriteRule ^(.*)index\.php$ /$1 [R=301,NC] 

8 Comments

what's the difference?
[r=301,nc] The [nc] specifies that the http host is case insensitive.
and what does L specifies?
OPs issue is not with uppercase/lowercase, it is with the site base.
L=Last ... no more rewrites will be processed after that one. I put a comment on the question about adding the RewriteBase directive
|
2

If your site is not at the root of the server (which it's not on your localhost), you will need to add a RewriteBase directive: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase

You need to add the line:

RewriteBase /mysite/

above the current lines in the htaccess on your localhost

1 Comment

is there could be any solution that will work on both side (i.e., localhost and server)
0

While RewriteBase directive stands for this usage, you may want to test for localhost and rewrite root to your subdirectory only if test == true. eg:

RewriteCond %{HTTP_HOST} ^localhost$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteRule (.*) /subdirectory/$1 [L]

Note I have not used this for some time and cannot guarantee you it'll work out of the box; might need some edit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.