【问题标题】:manage redirection of subfolder by htaccess通过 htaccess 管理子文件夹的重定向
【发布时间】:2017-10-05 22:34:44
【问题描述】:

我使用下面的.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

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite cakephp


    【解决方案1】:

    在站点根目录 .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
    

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

    【讨论】:

    • 它正在重定向但出现错误Controller class Staging could not be found.
    • 它有index.php,基本上服务器是engintron
    • 在更改 htaccess 相同的错误之后,在此之前它可以正常使用直接 url https://www.example.com/staging/
    • 好的,现在试试我更新的答案。我正在离开我的电脑,并能在几个小时后回来查看。
    • 你的意思是<IfModule mod_rewrite.c> RewriteOptions Inherit </IfModule>在暂存htaccess
    猜你喜欢
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多