强制http到https会在URL中附加“public”

时间:2016-09-15 18:26:43

标签: regex apache .htaccess mod-rewrite

我在.htaccess目录中有以下webroot

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^(/admin/)
    RewriteRule ^$ public/     [L]
    RewriteRule (.*) public/$1 [L]
</IfModule>

然后,在public目录中,我有以下.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On


    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/?$ index.php?$1 [QSA,L]

    # Force HTTPS
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


</IfModule>

然后,如果我点击以下网址:

http://example.com/

浏览器被重定向到:

https://example.com/public/

当通过HTTP加载站点(不强制)时,省略此部分。

1 个答案:

答案 0 :(得分:2)

强制https规则必须放在root .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On

   # Force HTTPS
    RewriteCond %{HTTPS} !on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

    RewriteRule ^$ public/     [L]

    RewriteCond %{REQUEST_URI} !^(/admin/)
    RewriteRule (.*) public/$1 [L]
</IfModule>

http->https删除public/.htaccess规则并清除浏览器cahce以对其进行测试。