2016-09-29 134 views
0

这让我疯狂。基本上,我想重定向如下:htaccess子域到子域/子文件夹

http://subdomain.mysite.com/(带或不带斜线) 到(完全一致) http://subdomain.mysite.com/subdomain/

目前这给了我一个无限循环

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

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

+0

当然,你必须添加一个条件,停止循环。 – arkascha

回答

1

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

请注意,如果您确实希望保持主机名不变,请在同一主机中重写,但仍然按照您的建议进行外部重定向。这多少有些令人吃惊,但绝对有可能:

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> /子/富

0

似乎并没有在工作所有。我知道它有点奇怪,但因为遗留原因。整个服务器mysite.com是旧的,只是为了存档的目的 - 并要求授权访问。除了/子域文件夹仍然需要访问。 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 

而且/子文件夹中,我只是说

Satisfy Any 
0

还好吧......我把它练到使用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/ - 所以我建立一个PHP的子域/子/夹重定向... 似乎混乱,但该网站是可见的,当你打开subdomain.mysite。 COM :)

+0

当然,这是可能的,但_why_? – arkascha