Doing external redirect. .htaccess NS flag not working. How to achieve similar results?
I have moved the site in a root's subdirectory, but want to keep the url's still working. I have already achieved this using following rule in htaccess:
RewriteCond %{REQUEST_URI} !^/subdir [NC]
RewriteRule ^(.*)$ /subdir/$1 [L,NS]
Now problem is that a given page (lets say index.php) can be accessed by two url's:
mysite.com/index.php
mysite.com/subdir/index.php
I don't want this to happen to avoid confusion for users and also to avoid same content being indexed twice by search engines. I am not too good with htaccess, but what I did was to do external redirect to my own site by creating another rule in htaccess:
RewriteRule ^subdir/(.*)$ http://mysite.com/$1 [L,R,NS,NC]
But problem is that this creates an endless loop through sub-requests. Normal request leads to sub-request for fetching page from "subdir" subdirectory, but that sub-request causes the second rule to do external redirect to my site, starting the loop all over again. NS flag, I found out later, is useless in case of external redirects. So what I wanted to ask is, how do I break this loop? OR how do I transfer the request made using "mysite.com/subdir/index.php" url to "mysite.com/ind开发者_JAVA百科ex.php"?
The first rule you posted is right
RewriteCond %{REQUEST_URI} !^/subdir [NC]
RewriteRule ^(.*)$ /subdir/$1 [L,NS]
The second rule you'll have to change. You should't do a Rewrite but a Redirect (preferably a 301).
This is helpful both in obtaining the desired results and in not getting your content indexed twice by search engines.
Since a redirect actually sends an http-response that states where the resource has moved it does not create an internal loop.
精彩评论