问题
This is driving me crazy. Basically, I want to redirect the following:
http://subdomain.mysite.com/ (with or without trailing slash) to (exactly) http://subdomain.mysite.com/subdomain/
This currently gives me an endless loop
RewriteCond %{HTTP_HOST} ^subdomain\.mysite\.com$ [NC]
RewriteRule ^(.*)$ subdomain/$1 [R=301]
But whatever rule I try, it always ends up in a loop because the target still matches the redirect criteria. Some help would be appreciated.
回答1:
You need to add a condition to break the endless loop.
Note that this loop will only arise if you really want to keep the host name unaltered, so rewrite inside the same host, but still do an external redirect as you suggest. This is somewhat surprising, but certainly possible:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain/ [NC]
RewriteRule ^(.*)$ subdomain/$1 [L,R=301,QSA]
This implements an external redirection, the additional condition is required to prevent the redirection loop:
https://subdomain.example.com/foo > https://subdomain.example.com/subdomain/foo
The same loop does not arise if you rewrite to another host name:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/subdomain/$1 [L,R=301,QSA]
This implements an external redirection, no additional condition is required, since the existing one already prevents the redirection loop:
https://subdomain.example.com/foo > https://www.example.com/subdomain/foo
A more often seen approach is to only rewrite internally, so without actually changing the URL visible in the browser:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain/ [NC]
RewriteRule ^(.*)$ subdomain/$1 [L,QSA]
This implements an internal redirection, so the visible URL in the client stays unchanged:
https://subdomain.example.com/foo > /subdomain/foo
回答2:
Doesn't seem to work at all. I know its a bit strange but its for legacy reasons. The whole server mysite.com is old and just for archive purpose - and requires authorization to access. Except the /subdomain folder which still needs to be accessible. mysite.com and subdomain.mysite.com point to the same home directory (historical reasons..)
With this setup http://subdomain.mysite.com/subdomain/ works perfectly fine ... but someone here really wants http://subdomain.mysite.com/ to work as well
This is my current htaccess
<FilesMatch "index.php">
AuthName "Protected Area"
AuthType Basic
AuthUserFile /home/www/.htpasswd
require valid-user
</FilesMatch>
#PIE.htc
AddType text/x-component .htc
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain/ [NC]
RewriteRule ^(.*)$ subdomain/$1 [L,R=301,QSA]
#more rules here
And inside the /subdomain folder I simply say
Satisfy Any
回答3:
Allright ... I got it to work with an external redirect with www.!
RewriteCond %{HTTP_HOST} ^subdomain\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.subdomain.mysite.com/subdomain/$1 [L,R=301,QSA]
Problem with this is, http://subdomain.mysite.com/subdomain/ now redirects to http://www.subdomain.mysite.com/subdomain/subdomain/ - so I setup a php redirect in the subdomain/subdomain/ folder ... it seems messy but the site is visible when you open subdomain.mysite.com :)
来源:https://stackoverflow.com/questions/39764287/htaccess-subdomain-to-subdomain-subfolder