[英]htaccess subdomain to subdomain/subfolder
这真让我抓狂。 基本上,我想重定向以下内容:
http://subdomain.mysite.com/ (带有或不带有斜杠)到(完全) http://subdomain.mysite.com/subdomain/
目前这给了我一个无尽的循环
RewriteCond %{HTTP_HOST} ^subdomain\.mysite\.com$ [NC]
RewriteRule ^(.*)$ subdomain/$1 [R=301]
但是无论我尝试使用什么规则,它总是以循环结尾,因为目标仍然符合重定向条件。 一些帮助,将不胜感激。
您需要添加条件来打破无限循环。
请注意,只有在您确实希望保持主机名不变的情况下才会出现此循环,因此请在同一主机内进行重写,但仍按照您的建议进行外部重定向。 这有些令人惊讶,但肯定有可能:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain/ [NC]
RewriteRule ^(.*)$ subdomain/$1 [L,R=301,QSA]
这实现了外部重定向,还需要附加条件来防止重定向循环:
https://subdomain.example.com/foo > https://subdomain.example.com/subdomain/foo
如果您重写为另一个主机名,则不会出现相同的循环:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/subdomain/$1 [L,R=301,QSA]
这实现了外部重定向, 不需要其他条件,因为现有条件已经可以防止重定向循环:
https://subdomain.example.com/foo > https://www.example.com/subdomain/foo
一种更常见的方法是仅在内部进行重写,因此无需实际更改浏览器中可见的URL:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain/ [NC]
RewriteRule ^(.*)$ subdomain/$1 [L,QSA]
这实现了内部重定向,因此客户端中的可见 URL保持不变:
https://subdomain.example.com/foo > / subdomain / foo
似乎根本不起作用。 我知道它有点奇怪,但出于传统原因。 整个服务器mysite.com很旧,仅用于存档目的-并需要授权才能访问。 / subdomain文件夹除外,该文件夹仍需要访问。 mysite.com和subdomain.mysite.com指向同一主目录(历史原因..)
通过这种设置, http://subdomain.mysite.com/subdomain/可以很好地工作...但是这里的人真的希望http://subdomain.mysite.com/也能正常工作
这是我目前的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
在/ subdomain文件夹中,我只是简单地说
Satisfy Any
好吧...我知道它可以与www一起使用外部重定向!
RewriteCond %{HTTP_HOST} ^subdomain\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.subdomain.mysite.com/subdomain/$1 [L,R=301,QSA]
问题是http://subdomain.mysite.com/subdomain/现在重定向到http://www.subdomain.mysite.com/subdomain/subdomain/-因此我在subdomain / subdomain /文件夹中设置了php重定向...似乎杂乱无章,但是打开subdomain.mysite.com时该站点可见:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.