通过htaccess管理子文件夹的重定向

时间:2017-10-05 08:28:51

标签: apache .htaccess redirect mod-rewrite cakephp

我在.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

1 个答案:

答案 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

在完全清除浏览器缓存后对其进行测试。