【问题标题】:RewriteCond not working on force https for HTTP_HOSTRewriteCond 无法对 HTTP_HOST 强制使用 https
【发布时间】:2014-06-15 12:37:18
【问题描述】:

我正在尝试编写一个条件,将在实时主机名 (domain.com) 上强制使用 https,但如果它在我们的本地测试主机名 (domain.local) 上则不会。

这是我所拥有的:

#force https
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !domain\.local [NC]
RewriteRule ^(.*?)$ https://www.domain.com/$1 [L,R=301]

此条件重写为 https,但也将 domain.local 重定向到 domain.com

我也尝试过这个条件来代替在 .local 或 .com 上没有任何作用的中间条件:

RewriteCond %{HTTP_HOST} ^domain\.com [NC]

这是我的 htaccess 文件的完整内容

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

#force ssl
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L,NE]

#rewrite payments subdomain
RewriteCond %{HTTP_HOST} ^payments\.domain\.local$ [NC]
RewriteRule ^(.*)$ https://otherdomain.com/dmi_domain.htm [NC,R=301,L]
RewriteCond %{HTTP_HOST} ^payments\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://otherdomain.com/loanadmin/dmi_domain.htm [NC,R=301,L]

#agents folder to subdomain
RewriteCond %{HTTP_HOST} ^agents\.domain\.local$ [NC]
RewriteRule ^agents/(.*)$ /agents/$1 [L,P]
RewriteCond %{HTTP_HOST} ^agents\.domain\.com$ [NC]
RewriteRule ^agents/(.*)$ /agents/$1 [L,P]

#force www for .com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^payments\. [NC]
RewriteCond %{HTTP_HOST} !^agents\. [NC]
RewriteCond %{HTTP_HOST} !^domain\.local
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{REQUEST_URI} !^/questions/
RewriteRule ^(.*?)$ https://www.%{HTTP_HOST}/$1 [L,R=301]

#force www for .local
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^payments\. [NC]
RewriteCond %{HTTP_HOST} !^agents\. [NC]
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{REQUEST_URI} !^/questions/
RewriteRule ^(.*?)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

#force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !^/rate-panel/xml/
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{REQUEST_URI} !^/questions/
RewriteRule ^(.*)$ $1/ [L,R=301]

#get rid of index.php in url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{REQUEST_URI} !^/questions/
RewriteRule ^(.*)$ /index.php/$1 [L]

谢谢

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite https


    【解决方案1】:

    您可以使用 RewriteCond %{ENV:HTTPS} off

    有时 https 标志不起作用。不知道为什么。但我找到了使用 ENV:HTTPS 的解决方案。这个停止重定向循环对我来说。

    【讨论】:

    • 这不是所有时间的工作。无论如何,您找到合适的解决方案了吗?
    • 我不确定,但可能。抱歉,如果我没有发布任何关于它的内容,我很专注。无论如何,感谢您的关注。
    • 您还有问题吗?
    • 不,不再。谢谢老兄。
    • 在我的情况下,我需要使用 RewriteCond %{ENV:HTTPS} !=on('off' 在我的特定环境中不起作用)。
    【解决方案2】:

    您需要将底部的 http->https 规则移动到 .htaccess 的顶部,就在 RewriteBase / 行的下方。也可以使用这个稍作修改的规则:

    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !local [NC]
    RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L,NE]
    

    【讨论】:

    • 谢谢,但还是不行。 .com 或 .local 上都没有任何反应。
    • 你能用最新的 .htaccess 更新你的问题吗?这是某种 PHP/CMS 框架吗?
    • @giovannipds %{HTTPS} 是标准的 Apache 变量。您可能在代理或防火墙后面。检查您的托管服务提供商的服务器文档。
    • 是的,但我现在发现问题出在我的托管服务器上(我在两个不同的服务器上比较了相同的代码)。无论如何,我仍在寻找替代方案。最好的问候。
    • 就我而言,我需要使用 RewriteCond %{ENV:HTTPS} !=on。所以下面提到的环境变量,特别是 '!=on' 而不是 'off'。
    【解决方案3】:

    许多状态,有时只有 RewriteCond %{ENV:HTTPS} !=on 有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      相关资源
      最近更新 更多