【问题标题】:My .htaccess redirect to https produces a 404我的 .htaccess 重定向到 https 会产生 404
【发布时间】:2016-01-22 17:14:55
【问题描述】:

我希望我的网络服务器将所有 http 请求重定向到 https。 所以我设置了我的 .htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

重定向似乎有效,但是当我输入 _http://example.com/test/ 时,它会重定向到 _https://example.com/httpdocs/test/ ,这会产生 404 错误。 如果我直接访问_https://example.com/test/,一切正常!

【问题讨论】:

    标签: apache .htaccess redirect https


    【解决方案1】:

    使用HTTP_HOST,而不是SERVER_NAME

    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L] 
    

    https://serverfault.com/questions/570288/is-it-bad-to-redirect-http-to-https

    另外,请确保您没有其他与此冲突的规则。您可能希望将此作为第一条规则进行推广(假设它具有 L 标志)。

    【讨论】:

    • 重写模式中不需要前导斜杠。
    【解决方案2】:

    感谢西蒙妮, 你的解决方案对我不起作用。

    我发现了

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    为我工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2016-06-05
      • 1970-01-01
      • 2022-01-11
      • 2021-11-10
      • 2012-12-23
      相关资源
      最近更新 更多