【问题标题】:How can I stop .htaccess redirecting a subdomain to the main site?如何停止 .htaccess 将子域重定向到主站点?
【发布时间】:2013-10-18 19:56:00
【问题描述】:

我有一个 .htaccess 问题。基本上我的 .htaccess 将我新创建的子域重定向到主站点,我不希望它这样做。假设我的域名为“www.beans.com”,子域为“shop.beans.com”,位于/shop/ 下的public_html 文件夹中。这是.htaccess:

DirectoryIndex index.php enmain.php

ErrorDocument 404 /404.html

## EXPIRES CACHING ##

ExpiresActive On
ExpiresByType image/jpg "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/ico "access plus 1 year"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType text/x-javascript "access plus 1 week"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType text/xml "access plus 2 hours" 
ExpiresDefault "access plus 1 hour"

## EXPIRES CACHING ##

RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.beans.com$ [NC]
    RewriteRule ^(.*)$ http://www.beans.com/$1 [L,R=301]

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?|php)(\?[^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]*/)*)index\.(html?|php)$  http://www.beans.com/$1  [R=301,L]
# Start CloudFlare:beans.com rewrite. Do not Edit 
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^beans.com 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 
# End CloudFlare rewrite. 

总而言之,我希望当我浏览“shop.beans.com”时不要被重定向到“www.beans.com”(目前正在发生这种情况)。

我该怎么做?

【问题讨论】:

    标签: .htaccess redirect url-rewriting subdomain


    【解决方案1】:

    首先你有两条规则做同样的事情,即添加www.

    1 - 删除此规则:

    RewriteCond %{HTTP_HOST} !^www.beans.com$ [NC]
    RewriteRule ^(.*)$ http://www.beans.com/$1 [L,R=301]
    

    2 - 然后像这样更改最后一条规则:

    RewriteCond %{HTTP_HOST} ^beans\.com$ [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 
    

    【讨论】:

      【解决方案2】:

      在您的 .htaccess 文件中必须有一条规则,您可以在其中设置将所有打开没有 www 的 url 重定向到使用 www。您无需更改此文件。

      你应该这样做:

      在您的子域中创建一个 .htaccess 文件并在其中添加以下代码。

      RewriteEngine on
      RewriteBase /
      

      保存此文件。 现在一切正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        • 1970-01-01
        • 2015-06-05
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多