74

我已经浏览了现有的问题,但我还没有真正遇到任何对我有用的东西。

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

我找到了在 .htaccess 文件中使用的这段代码。

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”或“ http:// ” www.example.co.uk '。

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

任何帮助和建议表示赞赏。

干杯。

4

17 回答 17

190

尝试以下操作:

 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。

于 2012-05-07T22:22:20.617 回答
16

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

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

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

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

于 2015-10-28T11:20:18.583 回答
5

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

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
于 2014-10-22T08:37:41.327 回答
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 班轮工作完美无缺)

于 2016-04-02T06:59:11.453 回答
3

我尝试了以上所有代码,但任何代码都不适用于我的网站。然后我尝试这段代码,这段代码非常适合我的网站。您可以在 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 更改为您的域名,并为我糟糕的英语感到抱歉。

于 2017-09-12T14:20:34.660 回答
3

对我来说,只工作这个变种:

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(俄语)

于 2018-04-06T08:56:16.447 回答
2
# 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]
于 2018-05-30T08:56:55.667 回答
1

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

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]
于 2016-03-26T17:05:48.830 回答
1

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

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

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

于 2016-05-06T08:10:19.217 回答
1

完美的代码转到 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]
于 2018-10-28T07:11:15.403 回答
1

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

RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^.*$ https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
于 2018-08-11T13:43:05.960 回答
1

如果您想将 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。

于 2016-10-01T06:20:04.043 回答
1

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

于 2018-09-08T23:06:19.607 回答
0

这些都不适合我,除了这个。我的网站托管在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]
于 2019-02-04T14:27:14.353 回答
0

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

SSL要求SSL

于 2020-12-02T15:46:52.380 回答
0

下面的代码,当添加到.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>

访问这里了解更多信息

于 2019-11-02T17:38:11.973 回答
0

出于某种原因,它在我的主机中倒置了,所以如果我想让它重新编辑,我需要检查 https 是否打开,然后重定向到 https

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

是我使用的代码

于 2022-02-22T17:55:17.247 回答