【问题标题】:.htaccess redirect http to https.htaccess 将 http 重定向到 https
【发布时间】:2012-11-02 19:05:25
【问题描述】:

我有一个旧网址 (www1.test.net),我想将其重定向到 https://www1.test.net
我已在我的网站上实施并安装了我们的 SSL 证书。
这是我的旧文件.htaccess

RewriteEngine On
RewriteRule !\.(js|gif|jpg|png|css|txt)$ public/index.php [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1 [L]

如何配置我的 .htaccess 文件,以便 url 自动重定向到 https
谢谢!

【问题讨论】:

  • 如果第二行不打算在路径指向实际文件的情况下进行重定向(并且您列出了预期的可能扩展名),则可以通过将其替换为更灵活检查现有文件:RewriteCond %{REQUEST_FILENAME} !-f ,然后是您的重定向。

标签: apache .htaccess


【解决方案1】:

我使用以下方法成功地将我的域的所有页面从 http 重定向到 https:

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

请注意,这将使用 301 'permanently moved' 重定向进行重定向,这将有助于转移您的 SEO 排名。

使用302 'temporarily moved' 更改[R=302,L] 进行重定向

【讨论】:

  • 这比“hacky”接受的答案更合适
  • 在某些情况下需要 X-Forwarded-Proto RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
  • 最好的答案在这里
  • 这在 Godaddy 服务器中对我有用。 @Bradley 太棒了!!
  • 这对我来说每次都适用于各种服务器环境和站点设置。应该是推荐的答案恕我直言。
【解决方案2】:

2016 年更新

由于这个答案受到了一些关注,我想提示一个更推荐的使用虚拟主机的方法:Apache: Redirect SSL

<VirtualHost *:80>
   ServerName mysite.example.com
   Redirect permanent / https://mysite.example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>

老答案,老生常谈 鉴于您的 ssl 端口未设置为 80,这将起作用:

RewriteEngine on

# force ssl
RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

请注意,这应该是您的第一个重写规则。

编辑:此代码执行以下操作。 RewriteCond(ition) 检查请求的 ServerPort 是否为 80(这是默认的 http-port,如果您指定了另一个端口,则必须调整条件)。如果是这样,我们匹配整个 url (.*) 并将其重定向到 https-url。 %{SERVER_NAME} 可以替换为特定的 url,但这样您就不必更改其他项目的代码。 %{REQUEST_URI} 是 TLD(顶级域)之后的 url 部分,因此您将被重定向到您来自的地方,但作为 https。

【讨论】:

  • 谢谢谢谢...它工作... :D 但是,你能解释它为什么工作吗?...谢谢... ^^
  • 很高兴它对你有用。检查更新的答案以获取有关代码的说明。这对你来说足够了吗?
  • "请注意,这应该是您的第一个重写规则。" @Jojo 救了我的命:*
  • 补充说明。 “[[L] 标志](httpd.apache.org/docs/2.4/rewrite/flags.html#flag_l) 导致 mod_rewrite 停止处理规则集。”通常你只想默认添加它。 “使用 [[R] 标志](httpd.apache.org/docs/2.4/rewrite/flags.html#flag_r) 会导致向浏览器发出 HTTP 重定向。”如果不存在,重定向将是内部的,这将使使用 HTTPS 的意义无效。默认情况下,发出的重定向状态为 302(临时重定向)。如果您希望浏览器(“永久”)缓存新 URL,请改用 [R=301]
  • 对于 VirtualHost 解决方案,如果设置复杂,则不清楚“永久重定向”语句应放置在何处。我假设接近顶部,它起作用了。
【解决方案3】:

这对于 www 和 HTTPS、代理和无代理用户来说是最好的。

RewriteEngine On

### WWW & HTTPS

# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

### WWW & HTTPS

【讨论】:

  • 如何重定向到非www?
  • 对于那些 %{HTTPS} off 不够用的人来说,这是迄今为止最好的答案
【解决方案4】:

我使用以下代码强制 https:

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

【讨论】:

  • @Niby 请检查我的回答日期和“已经存在的那个”。
【解决方案5】:

如果 HTTPS/SSL 连接在负载均衡器处结束并且所有流量都发送到端口 80 上的实例,则以下规则可用于重定向非安全流量。

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

确保 mod_rewrite 模块已加载。

【讨论】:

    【解决方案6】:

    寻找最佳的重定向方式,我发现了这个(来自 html5boilerplate):

    # ----------------------------------------------------------------------
    # | HTTP Strict Transport Security (HSTS)                              |
    # ----------------------------------------------------------------------
    
    # Force client-side SSL redirection.
    #
    # If a user types `example.com` in their browser, even if the server
    # redirects them to the secure version of the website, that still leaves
    # a window of opportunity (the initial HTTP connection) for an attacker
    # to downgrade or redirect the request.
    #
    # The following header ensures that browser will ONLY connect to your
    # server via HTTPS, regardless of what the users type in the browser's
    # address bar.
    #
    # (!) Remove the `includeSubDomains` optional directive if the website's
    # subdomains are not using HTTPS.
    #
    # http://www.html5rocks.com/en/tutorials/security/transport-layer-security/
    # https://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec-14#section-6.1
    # http://blogs.msdn.com/b/ieinternals/archive/2014/08/18/hsts-strict-transport-security-attacks-mitigations-deployment-https.aspx
    
    Header set Strict-Transport-Security "max-age=16070400; includeSubDomains"
    

    也许它会在 2017 年帮助某人! :)

    【讨论】:

    • 在 2017 年,这应该是公认的答案。但它应该包含通过 .htacces 文件启用 HSTS(问题带有此标记),您必须省略 IfModule 标记。如果你编辑你的答案,我很乐意从接受的答案链接到这里
    • 你好乔乔!抱歉这个迟到的答案,你的意思是同时删除 # &lt;IfModule mod_headers.c&gt;# &lt;/IfModule&gt; 行?
    • 嘿LIIT,是的,我就是这个意思。
    • 虽然这很好,并且确实有助于实现或支持(可能)预期的效果,但这将 http 请求重定向到 https,因此,不回答题。只有当客户端已经通过 HTTPS(在指定的 max-age 间隔内)访问该站点时,它才有效。所以它应该总是伴随着正确的重定向。
    【解决方案7】:

    将此代码添加到 .htaccess 文件的末尾

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

    【讨论】:

    • 可能是因为 mod_rewrite 在这个答案中没有像在其他答案中那样启用。
    • 只需将RewriteEngine on 添加到顶部即可。
    【解决方案8】:

    在您的 .htaccess 文件中插入此代码。它应该可以工作

    RewriteCond %{HTTP_HOST} yourDomainName\.com [NC]
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://yourDomainName.com/$1 [R,L]
    

    【讨论】:

      【解决方案9】:

      这确保重定向适用于所有不透明代理的组合。

      这包括 client proxy webserver 的情况。

      # behind proxy
      RewriteCond %{HTTP:X-FORWARDED-PROTO} ^http$
      RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
      
      # plain
      RewriteCond %{HTTP:X-FORWARDED-PROTO} ^$
      RewriteCond %{REQUEST_SCHEME} ^http$ [NC,OR]
      RewriteCond %{HTTPS} off
      RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
      

      【讨论】:

        【解决方案10】:

        使用以下代码,或者您可以阅读有关此主题的更多详细信息。 How to redirect HTTP to HTTPS Using .htaccess?

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

        【讨论】:

          【解决方案11】:

          我也遇到了重定向问题。我尝试了 Stackoverflow 上提出的所有建议。我自己发现的一个案例是:

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

          【讨论】:

            【解决方案12】:

            将以下内容添加到 .htaccess 的顶部

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

            【讨论】:

              【解决方案13】:

              重写引擎开启 RewriteCond %{HTTPS} 关闭 重写规则 (.*) https://%{HTTP_HOST}%{REQUEST_URI}

              How to htaccess file using redirect

              【讨论】:

              • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
              【解决方案14】:

              这就是最终为我工作的原因

              <IfModule mod_rewrite.c>
              RewriteEngine On
              RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
              </IfModule>
              

              【讨论】:

                【解决方案15】:

                使用 .htaccess 文件强制使用 HTTPS

                ==> 重定向所有网络流量:-

                要强制所有网络流量使用 HTTPS,请在您网站根文件夹的 .htaccess 文件中插入以下代码行。

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

                ==> 仅重定向指定域:-

                要强制特定域使用 HTTPS,请在您网站根文件夹的 .htaccess 文件中使用以下代码行:

                RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
                RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
                RewriteEngine On 
                RewriteCond %{HTTP_HOST} ^example\.com [NC]
                RewriteCond %{SERVER_PORT} 80 
                RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
                

                如果这不起作用,请尝试删除前两行。

                RewriteEngine On
                RewriteCond %{HTTP_HOST} ^example\.com [NC]
                RewriteCond %{SERVER_PORT} 80
                RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
                

                确保将 example.com 替换为您尝试使用的域名 强制https。此外,您需要将 www.example.com 替换为 您的实际域名。

                ==> 重定向指定文件夹:-

                如果您想对特定文件夹强制使用 SSL,请将以下代码插入到该特定文件夹中的 .htaccess 文件中:

                RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
                RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
                RewriteEngine On 
                RewriteCond %{SERVER_PORT} 80 
                RewriteCond %{REQUEST_URI} folder
                RewriteRule ^(.*)$ https://www.example.com/folder/$1 [R=301,L]
                

                确保将文件夹引用更改为实际文件夹名称。 然后确保将 www.example.com/folder 替换为您的实际域 您要强制启用 SSL 的名称和文件夹。

                【讨论】:

                  【解决方案16】:

                  domainname.com 替换你的域,它对我有用。

                  RewriteEngine On
                  RewriteCond %{HTTP_HOST} ^domainname\.com [NC]
                  RewriteCond %{SERVER_PORT} 80
                  RewriteRule ^(.*)$ https://www.domainname.com/$1 [R,L]
                  

                  【讨论】:

                  • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-11-08
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-03-18
                  • 2012-10-11
                  • 2015-11-10
                  相关资源
                  最近更新 更多