我有一个旧的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]
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为单位.
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)
Mut*_*ran 10
在 .htaccess 文件末尾添加此代码
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Run Code Online (Sandbox Code Playgroud)
我用以下代码强制使用https:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Run Code Online (Sandbox Code Playgroud)
如果 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模块已加载。
在寻找重定向的最佳方式时,我发现了这个(来自 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 年的某人有所帮助!:)
小智 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)
| 归档时间: |
|
| 查看次数: |
168171 次 |
| 最近记录: |