【问题标题】:htaccess subdomain to subdomain/subfolderhtaccess 子域到子域/子文件夹
【发布时间】:2016-09-29 16:25:23
【问题描述】:

这快把我逼疯了。基本上,我想重定向以下内容:

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

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

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

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

【问题讨论】:

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

标签: .htaccess redirect subdomain


【解决方案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]

这实现了一个外部重定向,需要no附加条件,因为现有的已经阻止了重定向循环:

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

【讨论】:

    【解决方案2】:

    似乎根本不起作用。我知道这有点奇怪,但出于遗留原因。整个服务器 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
    

    【讨论】:

      【解决方案3】:

      好的...我让它与带有 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 时该站点是可见的 :)

      【讨论】:

      • 当然,这是可能的,但是为什么
      猜你喜欢
      • 2011-02-20
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 2016-01-18
      • 2012-09-14
      • 2012-05-19
      • 2012-11-23
      • 2013-12-03
      相关资源
      最近更新 更多