我在.htaccess
下面使用我的网站从http
重定向到https://www
。
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
我有一个子文件夹staging
我希望每次访问该文件夹时都应该重定向到https://www.example.com/staging
。
我尝试使用以下代码添加到我的根htaccess
RewriteCond %{HTTPS} off
RewriteRule ^(staging/.*)$ https://www.%{HTTP_HOST}/$1
并且
RewriteCond %{HTTPS} !=on
RewriteRule ^(staging/.*)$ https://www.example.com/$1
但是,如果我尝试不使用https://www.example.com/
,我的网站每次都会重定向到https://www
http://example.com/staging
到
https://www.example.com/
我的子文件夹的Htaccess是
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
服务器是engintron
答案 0 :(得分:0)
在站点根目录中使用此代码.htaccess:
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]
RewriteRule ^(.*)$ webroot/$1 [L]
从staging/.htaccess
文件中输入此代码:
RewriteOptions Inherit
在完全清除浏览器缓存后对其进行测试。