116

我有一个旧www1.test.net网址https://www1.test.net

这是我的旧文件.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
谢谢!

4

16 回答 16

230

我使用以下内容成功地将我的域的所有页面从 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]

于 2014-11-19T23:37:23.617 回答
130

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-port 未设置为 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。

于 2012-11-14T09:56:30.747 回答
81

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
于 2016-09-09T16:11:17.130 回答
14

我使用以下代码强制 https:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
于 2012-11-14T09:59:36.157 回答
6

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

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

确保mod_rewrite模块已加载。

于 2016-09-02T23:14:00.060 回答
4

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

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
于 2012-11-14T09:40:27.383 回答
4

寻找重定向的最佳方式,我发现了这个(来自 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-01-12T14:38:30.770 回答
4

将此代码插入到您的 .htaccess 文件中。它应该工作

RewriteCond %{HTTP_HOST} yourDomainName\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourDomainName.com/$1 [R,L]
于 2018-11-13T09:14:28.473 回答
3

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

这包括案例客户端 <http> 代理 <https> 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]
于 2020-08-30T15:33:42.797 回答
3

使用以下代码,或者您可以阅读有关此主题的更多详细信息。 如何使用 .htaccess 将 HTTP 重定向到 HTTPS?

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
于 2021-04-28T08:31:16.177 回答
2

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

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP:SSL} !=1 [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
于 2016-05-14T09:22:20.970 回答
2

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

RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
于 2018-08-03T13:40:50.337 回答
1

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

如何使用重定向 htaccess 文件

于 2021-09-19T16:50:49.587 回答
0

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

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
于 2021-07-06T01:46:30.597 回答
-1

.htaccess使用文件强制 HTTPS

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

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

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 的实际域名和文件夹。

于 2020-06-07T20:31:48.203 回答
-1

将您的域替换为domainname.com,它对我有用。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainname\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domainname.com/$1 [R,L]
于 2020-08-21T21:57:44.670 回答