【问题标题】:Https to http with variant exceptions带有变体异常的 Https 到 http
【发布时间】:2017-08-31 17:01:49
【问题描述】:

我们的网站即将切换到 SSL,但我们还没有准备好。因此,我们将在 htaccess 中将所有 https URL 重定向到 http。我已经完成了这项工作,但作为一个额外的复杂因素,我们有一些 URL 必须是重定向的例外。这些网址是: somebody.dev.www.example.com example.com/top_deal example.com/watches

我的重定向是这样工作的: RewriteCond %{HTTP:X-Forwarded-Proto} =https RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]

但无法使异常正常工作。我已经尝试了几乎所有我能找到的在线解决方案。

【问题讨论】:

    标签: apache .htaccess https url-redirection http-redirect


    【解决方案1】:

    您可以使用规则作为您的第一条规则:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !=somebody.dev.www.example.com
    RewriteCond %{THE_REQUEST} !\s/+(?:top_deal|watches)/?[?\s] [NC]
    RewriteCond %{HTTP:X-Forwarded-Proto} =https
    RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    # rest of your rules go here
    

    确保在测试前清除浏览器缓存。

    【讨论】:

    • 但是如果有其他子域不是example.com,但也使用URI /watchestop_deal
    • 我只想在一个规则中做到这一点。如果您写的内容是可能的,则需要将其分解为 2 个单独的规则。
    • 这不起作用。直接去https。根本没有重定向。
    • 您在浏览器中输入并重定向到https 的确切http URL 是什么?你清除浏览器缓存了吗?
    【解决方案2】:

    首先,您希望它是临时重写 (=302) 还是浏览器应将其保存为 permanent 并因此得到 301?只是问,两者都有效,但如果你想暂时去,你应该阅读this

    Excerpt: HTTP/1.1 (RFC 2616) added the new status codes 303 and 307

    所以在你的情况下,307 可能会更好。

    如果您想要永久重写,请使用[R=301] 使浏览器缓存重写目标,而不是每次都重写。


    现在到 错误 解决方案(但我离开那里有一个好的原因,请参阅here) - 不要使用它! !

    #logic is: 1 OR (2 AND 3) --> NO, it isn't, see the link right above!
    #1 being hostname: somebody.dev.www.example.com
    #2 being hostname: example.com
    #3 being REQUEST_URI = !^/(top_deal|watches)
    RewriteCond %{HTTP_HOST} ^somebody.dev.www.example.com$ [OR]
    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteCond %{REQUEST_URI} !^/(top_deal|watches)$
    RewriteRule .? - [END]
    ##[END] makes the rewrite-evaluation STOP, completely, not only for this turn!
    
    #same rule as before, just changed 302 -> 307 (RFC 2616) and ^(.*)$ to .? cuz I like it better^^
    RewriteCond %{HTTP:X-Forwarded-Proto} =https
    RewriteRule .? http://%{HTTP_HOST}%{REQUEST_URI} [L,R=307]
    

    更新

    下面是真正应该放入配置的内容:

    # Don't rewrite HOST 'somebody.dev.www.example.com'
    RewriteCond %{HTTP_HOST} ^somebody.dev.www.example.com$
    RewriteRule ^ - [END]
    
    # Don't rewrite HOST 'example.com', if URI 'top_deal' OR 'watches'
    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteCond %{REQUEST_URI} ^/(top_deal|watches)$
    RewriteRule ^ - [END]
    
    RewriteCond %{HTTPS} on
    RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=307]
    

    这绝对可以完成这项工作:)

    如果没有,由于某种原因,搜索您的LogLevel 并将rewrite:traceX 添加到该行。所以如果你的LogLevelerror 它应该是这样的:

    LogLevel error rewrite:trace2
    

    然后重新启动/重新加载您的服务器并再次测试。 Error.log 现在将显示尝试重写的结果。使用tail -f error_log|fgrep '[rewrite:' 找到它们。如果他们不清楚,请告诉我们,也许我们可以提供帮助:)

    【讨论】:

    • 这不起作用。我收到 500 内部服务器错误...这比我一天半以来一直没有遇到的问题要好。
    • 添加了一些解释,一个正确的答案,希望对进一步的故障排除有所帮助
    【解决方案3】:

    我最终完成的工作如下:

    RewriteCond %{HTTP_HOST} !^(.*).dev.www.example.com
    RewriteCond %{REQUEST_FILENAME} ! top_deal(.*)
    RewriteCond %{REQUEST_FILENAME} ! watches(.*)
    RewriteCond %{HTTP:X-Forwarded-Proto} =https
    RewriteRule .? http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 2021-05-10
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多