htaccess 子域到 subdomain/subfolder

htaccess subdomain to subdomain/subfolder

这让我发疯。基本上,我想重定向以下内容:

http://subdomain.mysite.com/(带或不带斜杠) 到(确切地) http://subdomain.mysite.com/subdomain/

目前这让我陷入无限循环

    RewriteCond %{HTTP_HOST} ^subdomain\.mysite\.com$ [NC]  
    RewriteRule ^(.*)$ subdomain/ [R=301]

但无论我尝试什么规则,它总是以循环结束,因为目标仍然符合重定向条件。一些帮助将不胜感激。

您需要添加一个条件来打破死循环。

请注意,只有当您真的想保持主机名不变时才会出现此循环,因此在同一主机内重写,但仍然按照您的建议进行外部重定向。这有点令人惊讶,但肯定有可能:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]  
RewriteCond %{REQUEST_URI} ^/subdomain/ [NC]
RewriteRule ^(.*)$ subdomain/ [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/ [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/ [L,QSA]

这实现了一个内部重定向,所以客户端中的visible URL保持不变:

https://subdomain.example.com/foo > /subdomain/foo

似乎根本不起作用。我知道它有点奇怪,但它是出于遗留原因。整个服务器 mysite.com 很旧,仅用于存档目的 - 需要授权才能访问。除了仍然需要访问的 /subdomain 文件夹。 mysite.com 和 subdomain.mysite.com 指向同一个主目录(历史原因..)

使用此设置 http://subdomain.mysite.com/subdomain/ works perfectly fine ... but someone here really wants 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/ [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/ [L,R=301,QSA]

问题在于,http://subdomain.mysite.com/subdomain/ now redirects to http://www.subdomain.mysite.com/subdomain/subdomain/ - 所以我在 subdomain/subdomain/ 文件夹中设置了一个 php 重定向 ... 看起来很乱,但是当您打开 subdomain.mysite.com :)

时可以看到该站点