我想用https和www重定向我的网站。我想从url中删除尾随的破折号,并强制使用https重定向,如果给定test.com,则需要使用www.test.com重定向。
http://www.test.com -> https://www.test.com
http://test.com -> https://www.test.com
http://test.com/ -> https://www.test.com 我已经试过了
#Trailing Slash Removal
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|offs()
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^test.com [NC]
RewriteRule ^(.*)$ https://www.test.com/$1 [L,R=301]
</IfModule>现在使用http://test.com将重定向正确地重定向到https://www.test.com,但https://test.com将作为This site can’t provide a secure connection通过错误如何重定向
发布于 2020-01-24 15:29:36
像下面这样的方法有多种。
将HTTP重定向到HTTPS
1.重定向所有网络流量如果您的.htaccess中已有代码,请添加以下内容:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]HTTPS 2.仅重定向特定域名要重定向特定域名以使用,请添加以下内容:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]HTTPS 3.仅重定向特定文件夹重定向到特定文件夹上的,添加以下内容:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]发布于 2020-01-24 17:05:06
您可以在一条规则中同时执行http -> https和non-www to www,而无需对域名进行硬编码。
在站点根目录、htaccess或Apache配置中使用此规则:
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]https://stackoverflow.com/questions/59891920
复制相似问题