Php 使用https和www强制重定向

Php 使用https和www强制重定向,php,.htaccess,Php,.htaccess,我想用https和www重定向我的网站。我想从url中删除拖尾shash并强制用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

我想用https和www重定向我的网站。我想从url中删除拖尾shash并强制用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>
#尾部斜杠删除
重新启动发动机
重写cond%{HTTP_HOST}^www\。
在| offs()上重写cond%{HTTPS}s
重写规则^http%1://www.%{http_HOST}%{REQUEST_URI}[NE,L,R]
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)/$/$1[L,R=301]
RewriteCond%{HTTP_HOST}^test.com[NC]
重写规则^(.*)$https://www.test.com/$1[L,R=301]

现在可以正确地使用重定向,但会出错,因为
此站点无法提供安全连接
如何使其重定向

有以下多种方法

将HTTP重定向到HTTPS

1。重定向所有Web流量 如果.htaccess中有现有代码,请添加以下代码:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
2。仅重定向特定域 要重定向特定域以使用HTTPS,请添加以下内容:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]
3。仅重定向特定文件夹 重定向到特定文件夹上的HTTPS时,添加以下内容:

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

您可以在一条规则中同时执行
http->https
非www到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]

确保这是您的最重要的规则,并删除处理
http->https
www

的所有其他规则。如果我已向您提供了完整的代码,请更改我的完整模块。只需将所有有问题的代码替换为此代码,并在新浏览器中测试。请测试。
http://test.com/ 
将由浏览器本身自动删除。哪个URL未按预期重定向?您遇到了什么错误?发现此站点无法提供安全连接错误