【问题标题】:http to https through .htaccesshttp 到 https 通过 .htaccess
【发布时间】:2023-04-12 23:51:01
【问题描述】:

我已经浏览了现有的问题,但我还没有真正遇到任何适合我的问题。

我目前正在使用安全 SSL 证书运行一个站点。可以通过https://www.example.co.uk 访问它,但问题是该站点也可以通过http://www.example.co.uk 访问——我不希望这成为可能。我需要它从 http 重定向到 https。

我找到了这个在 .htaccess 文件中使用的 sn-p 代码。

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example.co.uk [NC] 
RewriteRule ^(.*)$ https://example.co.uk/$1 [L,R=301]

当用户在地址栏中输入 example.co.uk 时,这可以正常工作,但我还需要添加某种条件语句,以便如果用户输入“www.example.co.uk”或“@” 987654323@'。

我尝试过使用 [OR] 之类的方法,但这最终会导致服务器错误。

感谢任何帮助和建议。

干杯。

【问题讨论】:

标签: http .htaccess redirect https


【解决方案1】:

尝试以下方法:

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

另外,还可以根据端口号进行重定向,例如:

 RewriteCond %{SERVER_PORT} ^80$
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

这会将端口 80 上收到的所有请求重定向到 HTTPS。

【讨论】:

    【解决方案2】:

    在 .htaccess 文件中添加以下代码。

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

    将 example.com 更改为您的网站域

    网址重定向教程可以从这里找到 - Redirect non-www to www & HTTP to HTTPS using .htaccess file

    【讨论】:

      【解决方案3】:

      试试这个,我用过,效果很好

      Options +FollowSymLinks
      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
      

      【讨论】:

        【解决方案4】:

        试试这个:

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

        来源: https://www.ndchost.com/wiki/apache/redirect-http-to-https

        (我尝试了这么多不同的代码块,这 3 行完美无缺)

        【讨论】:

          【解决方案5】:

          我尝试了上述所有代码,但任何代码都不适用于我的网站。然后我尝试这段代码,这段代码对我的网站运行完美。您可以在 htaccess 中使用以下规则:

          <IfModule mod_rewrite.c>
          Options +FollowSymLinks
          RewriteEngine On
          
          //Redirect http to https
          RewriteCond %{SERVER_PORT} 80
          RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
          RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
          
          //Redirect non-www to www
          RewriteCond %{HTTP_HOST} ^example.com [NC]
          RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
          
          </IfModule>
          

          用你的域名更改example.com,对不起我的英语不好。

          【讨论】:

            【解决方案6】:

            对我来说只能使用这个变体:

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

            谢谢https://www.reg.ru/support/hosting-i-servery/sajty-i-domeny/kak-dobavit-redirekt/redirekt-s-http-na-https(俄语)

            【讨论】:

              【解决方案7】:
              # Switch rewrite engine off in case this was installed under HostPay.
              RewriteEngine Off
              
              SetEnv DEFAULT_PHP_VERSION 7
              
              DirectoryIndex index.cgi index.php
              
              # BEGIN WordPress
              <IfModule mod_rewrite.c>
              RewriteEngine On
              RewriteBase /
              RewriteRule ^index\.php$ - [L]
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteRule . /index.php [L]
              </IfModule>
              
              # END WordPress
              
              # RewriteCond %{HTTP_HOST} ^compasscommunity.co.uk\.com$ [NC]
              # RewriteRule ^(.*)$ https://www.compasscommunity.co.uk/$1 [L,R=301]
              

              【讨论】:

                【解决方案8】:

                要以简单的方式重定向http://example.com http://www.example.com https://www.example.com,您可以在htaccess中使用以下规则:

                RewriteEngine on
                
                RewriteCond %{HTTPS} off
                RewriteCond www.%{HTTP_HOST} ^(?:www\.)?(www\..+)$ [NC]
                RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
                

                [已测试]

                %{REQUEST_SCHEME} 变量自 apache 2.4 起可用,此变量包含请求方案的值(http 或 https),在 apache 2.4 上您可以使用以下规则:

                RewriteEngine on
                
                
                RewriteCond %{REQUEST_SCHEME} ^http$
                RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$ [NC]
                RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
                

                【讨论】:

                  【解决方案9】:

                  有更好、更安全的方法来确保您的所有流量都超过https。例如设置两个虚拟主机并将所有流量从您的http 重定向到您的https 主机。在this answer here on security.stackexchange.com 中阅读更多信息。

                  通过设置虚拟主机进行重定向,您可以发送 301 状态(永久重定向),以便浏览器了解以下所有请求都应发送到重定向到的 https 服务器。因此,在第一次重定向响应之后不会再发出 http 请求。

                  您还应该仔细检查给定的答案,因为如果设置了错误的重写规则,您可能会丢失传入请求中的查询参数。

                  【讨论】:

                    【解决方案10】:

                    如果您想将 HTTP 重定向到 HTTPS 并希望在每个 URL 中添加 www,请使用下面的 htaccess

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

                    它会先将 HTTP 重定向到 HTTPS,然后再重定向到 www。

                    【讨论】:

                      【解决方案11】:

                      由于这是搜索中的*结果之一,如果您尝试在 AWS beanstalk 上添加 http 到 https 重定向,则接受的解决方案将不起作用。 您需要以下代码:

                      RewriteCond %{HTTP:X-Forwarded-Proto} =http
                      RewriteRule ^.*$ https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
                      

                      【讨论】:

                        【解决方案12】:

                        试试这个 RewriteCond %{HTTP_HOST} !^www. [NC] RewriteRule ^(.*)$ @987654321@{HTTP_HOST}/$1 [R=301,L]

                        【讨论】:

                          【解决方案13】:

                          完美的代码转到 HTML 索引:

                          RewriteEngine on
                          RewriteCond %{HTTP_HOST} ^YourNameWebsite\.com$ [OR]
                          RewriteCond %{HTTP_HOST} ^www\.YourNameWebsite\.com$
                          RewriteRule ^/?$ "https\:\/\/YourNameWebsite\.com\/index\.html" [R=301,L]
                          

                          或者

                          完美的代码进入 PHP 索引:

                          RewriteEngine on
                          RewriteCond %{HTTP_HOST} ^YourNameWebsite\.com$ [OR]
                          RewriteCond %{HTTP_HOST} ^www\.YourNameWebsite\.com$
                          RewriteRule ^/?$ "https\:\/\/YourNameWebsite\.com\/index\.php" [R=301,L]
                          

                          【讨论】:

                            【解决方案14】:

                            这些都不适合我,除了这个。我的网站托管在https://www.asmallorange.com

                            RewriteEngine On
                            RewriteCond %{HTTPS} !on
                            RewriteCond %{SERVER_PORT} !^443$
                            RewriteCond %{HTTP:X-Forwarded-Proto} !https
                            RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
                            

                            【讨论】:

                              【解决方案15】:

                              以下代码,当添加到.htaccess 文件时,会自动将所有发往 http: 的流量重定向到 https:

                              <IfModule mod_rewrite.c>
                                  RewriteEngine On
                                  RewriteCond %{HTTPS} off
                                  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
                              </IfModule>
                              

                              如果您的项目在Laravel 中添加这两行

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

                              就在RewriteEngine On 的下方。最后,您的.htaccess 文件将如下所示。

                              <IfModule mod_rewrite.c>
                                  <IfModule mod_negotiation.c>
                                      Options -MultiViews -Indexes
                                  </IfModule>
                              
                                  RewriteEngine On
                              
                                  RewriteCond %{HTTPS} off
                                  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
                              
                                  # Handle Authorization Header
                                  RewriteCond %{HTTP:Authorization} .
                                  RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
                              
                                  # Redirect Trailing Slashes If Not A Folder...
                                  RewriteCond %{REQUEST_FILENAME} !-d
                                  RewriteCond %{REQUEST_URI} (.+)/$
                                  RewriteRule ^ %1 [L,R=301]
                              
                                  # Handle Front Controller...
                                  RewriteCond %{REQUEST_FILENAME} !-d
                                  RewriteCond %{REQUEST_FILENAME} !-f
                                  RewriteRule ^ index.php [L]
                              </IfModule>
                              

                              Visite here to more information

                              【讨论】:

                                【解决方案16】:

                                在 .htaccess 文件中添加以下行以限制 http 访问

                                SSL要求SSL

                                【讨论】: