简体   繁体   English

.htaccess 重定向 http 到 https

[英].htaccess redirect http to https

I have an old url ( www1.test.net ) and I would like to redirect it to https://www1.test.net我有一个旧的 url ( www1.test.net ),我想将它重定向到https://www1.test.net
I have implemented and installed our SSL certificate on my site.我已经在我的网站上实施并安装了我们的 SSL 证书。
This is my old file .htaccess :这是我的旧文件.htaccess

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

How can I configure my .htaccess file so that url auto redirect to https ?如何配置我的.htaccess文件,以便 url 自动重定向到https
Thanks!谢谢!

I use the following to successfully redirect all pages of my domain from http to https:我使用以下方法成功地将我域的所有页面从 http 重定向到 https:

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

Note this will redirect using the 301 'permanently moved' redirect, which will help transfer your SEO rankings.请注意,这将使用301 'permanently moved'重定向进行重定向,这将有助于转移您的 SEO 排名。

To redirect using the 302 'temporarily moved' change [R=302,L]使用302 'temporarily moved'更改重定向[R=302,L]

Update 2016 2016 年更新

As this answer receives some attention, I want to hint to a more recommended way on doing this using Virtual Hosts: Apache: Redirect SSL由于这个答案受到了一些关注,我想提示一种更推荐的使用虚拟主机执行此操作的方法: 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>

Old answer, hacky thing given that your ssl-port is not set to 80, this will work:旧答案,鉴于您的 ssl-port 未设置为 80,这会起作用:

RewriteEngine on

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

Note that this should be your first rewrite rule.请注意,这应该是您的第一个重写规则。

Edit: This code does the following.编辑:此代码执行以下操作。 The RewriteCond(ition) checks wether the ServerPort of the request is 80 (which is the default http-port, if you specified another port, you would have to adjust the condition to it). RewriteCond(ition) 检查请求的 ServerPort 是否为 80(这是默认的 http 端口,如果您指定了另一个端口,则必须对其进行调整)。 If so, we match the whole url (.*) and redirect it to a https-url.如果是这样,我们匹配整个 url (.*)并将其重定向到 https-url。 %{SERVER_NAME} may be replaced with a specific url, but this way you don't have to alter the code for other projects. %{SERVER_NAME}可以替换为特定的 url,但这样您就不必更改其他项目的代码。 %{REQUEST_URI} is the portion of the url after the TLD (top-level-domain), so you will be redirected to where you came from, but as https. %{REQUEST_URI}是 TLD(顶级域)之后的 url 部分,因此您将被重定向到您来自的位置,但作为 https。

This is the best for www and for HTTPS, for proxy and no proxy users.这对于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

I force the https with following code:我使用以下代码强制 https:

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

In cases where the HTTPS/SSL connection is ended at the load balancer and all traffic is sent to instances on port 80, the following rule works to redirect non-secure traffic.如果 HTTPS/SSL 连接在负载均衡器处结束并且所有流量都发送到端口 80 上的实例,则以下规则可用于重定向非安全流量。

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

Ensure the mod_rewrite module is loaded.确保mod_rewrite模块已加载。

Add this code at the end of your .htaccess file将此代码添加到 .htaccess 文件的末尾

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

Searching for the best way to redirect, i've found this (coming from html5boilerplate) :寻找重定向的最佳方式,我发现了这个(来自 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"

Maybe it will help someone in 2017 !也许它会在 2017 年帮助某人! :) :)

Insert this code in your .htaccess file.将此代码插入您的 .htaccess 文件中。 And it should work它应该工作

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

This makes sure that redirects work for all combinations of intransparent proxies.这确保重定向适用于所有不透明代理的组合。

This includes the case client <http> proxy <https> webserver .这包括案例客户端 <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]

I had a problem with redirection also.我也有重定向问题。 I tried everything that was proposed on Stackoverflow.我尝试了 Stackoverflow 上提出的所有内容。 The one case I found by myself is:我自己发现的一个案例是:

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

Adding the following to the top of the .htaccess将以下内容添加到 .htaccess 的顶部

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

Use the following code or you can read more in detail on this topic.使用以下代码,或者您可以阅读有关此主题的更多详细信息。 How to redirect HTTP to HTTPS Using .htaccess? 如何使用 .htaccess 将 HTTP 重定向到 HTTPS?

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

The below code, when added to the .htaccess file, will automatically redirect any traffic destined for http: to https: 将下面的代码添加到.htaccess文件后,它将自动将发往http:的所有流量重定向到https:

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

Visite here to more information 访问此处了解更多信息

This is what ended up working for me这就是最终为我工作的原因

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Forcing HTTPS with the .htaccess File使用.htaccess文件强制使用 HTTPS

==> Redirect All Web Traffic :- ==> 重定向所有网络流量:-

To force all web traffic to use HTTPS, insert the following lines of code in the .htaccess file in your website's root folder.要强制所有网络流量使用 HTTPS,请在网站根文件夹的.htaccess文件中插入以下代码行。

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

==> Redirect Only Specified Domain :- ==> 仅重定向指定域:-

To force a specific domain to use HTTPS, use the following lines of code in the .htaccess file in your website's root folder:要强制特定域使用 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]

If this doesn't work, try removing the first two lines.如果这不起作用,请尝试删除前两行。

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

Make sure to replace example.com with the domain name you're trying force to https.确保将 example.com 替换为您尝试强制使用 https 的域名。 Additionally, you need to replace www.example.com with your actual domain name.此外,您需要将 www.example.com 替换为您的实际域名。

==> Redirect Specified Folder :- ==> 重定向指定文件夹:-

If you want to force SSL on a specific folder, insert the code below into a .htaccess file placed in that specific folder:如果要在特定文件夹上强制使用 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]

Make sure you change the folder reference to the actual folder name.确保将文件夹引用更改为实际文件夹名称。 Then be sure to replace www.example.com/folder with your actual domain name and folder you want to force the SSL on.然后确保将 www.example.com/folder 替换为您要强制启用 SSL 的实际域名和文件夹。

Replace your domain with domainname.com , it's working with me .domainname.com替换您的域,它正在与我合作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM