.htaccess将http重定向到https

Bàn*_*rần 73 apache .htaccess

我有一个旧的url(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]
Run Code Online (Sandbox Code Playgroud)

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

Bra*_*ood 136

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

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

请注意,这将使用301 'permanently moved'重定向重定向,这将有助于转移您的搜索引擎优化排名.

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

  • 这比"hacky"接受的答案更合适 (4认同)
  • 在某些情况下,需要X-Forwarded-Proto`RewriteCond%{HTTP:X-Forwarded-Proto}!https RewriteCond%{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R = 301,NE]` (3认同)

Joj*_*ojo 99

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>
Run Code Online (Sandbox Code Playgroud)

老答案,hacky事情, 因为你的ssl-port没有设置为80,这将工作:

RewriteEngine on

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

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

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

  • 添加到解释中。“[L] 标志](https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_l) 导致 mod_rewrite 停止处理规则集。” 通常您只想默认添加它。“使用 [R] 标志](https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_r) 会导致向浏览器发出 HTTP 重定向。” 如果不存在,重定向将是内部的,这将使使用 HTTPS 的意义失效。默认情况下,发出的重定向的状态为 302(临时重定向)。如果您希望浏览器(“永久”)缓存新 URL,请改用“[R=301]”。 (2认同)

lli*_*oor 45

这是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
Run Code Online (Sandbox Code Playgroud)

  • 如何重定向到非www? (2认同)

Mut*_*ran 10

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

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Run Code Online (Sandbox Code Playgroud)


Ard*_*rda 8

我用以下代码强制使用https:

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


Ene*_*nso 6

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

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

确保mod_rewrite模块已加载。


LII*_*IIT 5

在寻找重定向的最佳方式时,我发现了这个(来自 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"
Run Code Online (Sandbox Code Playgroud)

也许它会对 2017 年的某人有所帮助!:)

  • 虽然这很好,并且确实有助于实现或支持(可能)期望的效果,但这_不会_将 http 请求重定向到 https,因此不能回答问题。仅当客户端已通过 HTTPS 访问站点(在指定的 max-age 间隔内)时,它才起作用。因此,它应该始终伴随着正确的重定向。 (3认同)

小智 5

将此代码插入您的 .htaccess 文件中。它应该有效

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