【问题标题】:excluding a subdomain from htaccess redirect从 htaccess 重定向中排除子域
【发布时间】:2014-10-24 06:33:39
【问题描述】:

在提交这个帖子之前我已经搜索了stackoverflow,但我没有运气:-S

我有一个包含复杂 .htaccess 文件的网站,如下所示:

这部分用于重定向(X.2nate dot com --> 2nate dot com/api/x),效果很好

########### API Redirection Section ##########

RewriteEngine On
 # Extract the subdomain part of domain.com
RewriteCond %{HTTP_HOST} ^([^\.]+)\.2nate\.com$ [NC]
 # Check that the subdomain part is not www and ftp and mail
RewriteCond %1 !^(www|ftp|mail|charge)$ [NC]
 # Redirect all requests to the original url  /blog
RewriteRule ^.*$ https://2nate.com/api/%1 [L]

这部分用于将非 SSL 请求重定向到 SSL 协议:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

这部分是用WWW重定向到非WWW地址:

RewriteCond %{HTTP_HOST} ^www\.2nate\.com$
RewriteRule ^/?$ "http\:\/\/2nate\.com\/" [R=301,L]

问题是:

当我尝试访问“charge” dot mywebsite dot com(charge 是我主机中的一个真实子域)时,我无法访问相关页面并且它正在重定向到主页!

我认为这部分有问题:

RewriteCond %{HTTP_HOST} ^www\.2nate\.com$
RewriteRule ^/?$ "http\:\/\/2nate\.com\/" [R=301,L]

因为当我删除这部分时,一切正常(重定向到非 WWW 除外)。

感谢任何帮助。

【问题讨论】:

    标签: .htaccess mod-rewrite redirect ssl wildcard


    【解决方案1】:

    问题其实是这条规则:

    RewriteRule ^.*$ https://2nate.com/api/%1 [L]
    

    由于您在目标 URL 中使用 http://,因此它不是静默重写,而是会变成外部重定向。

    让你的 .htaccess 像这样:

    RewriteEngine On
    
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    RewriteCond %{HTTP_HOST} ^www\.2nate\.com$
    RewriteRule ^ http://2nate.com%{REQUEST_URI} [R=301,L,NE]
    
    # Extract the subdomain part of domain.com
    RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.2nate\.com$ [NC]
    # Check that the subdomain part is not www and ftp and mail
    RewriteCond %1 !^(www|ftp|mail|charge)$ [NC]
     # Redirect all requests to the original url /blog
    RewriteRule ^.*$ /api/%1 [L]
    

    【讨论】:

    • 感谢您抽出宝贵时间,但它没有按我想要的方式工作。它只是影响静默/外部重定向(我不想更改这些设置)。我只想访问charge.2nate.com。
    • 我有另一个真正的子域“blog”-dot-mywebsite-dot-com,它运行良好。
    • 我想我现在没有遇到您的问题?哪个网址不适合您?
    • 另外https://charge.2nate.com/add-event.html 对我来说工作得很好,它并没有改变到主域。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 2010-11-22
    • 2023-03-17
    相关资源
    最近更新 更多