在htaccess中强制使用HTTPS,同时也仅使公用文件夹可访问

时间:2018-08-14 16:09:55

标签: .htaccess

.htaccess文件在我的/ public_html /文件夹中:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ public/ [L]
    RewriteRule (.*) public/$1 [L]
</IfModule>

这个.htaccess在我的/ public_html / public /文件夹中

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

我需要添加如下规则:

RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

我遇到了重定向过多次的问题。

我仍然需要使用公用文件夹作为我的根目录,并且仍要使用/ public /中的前端控制器,但我想将所有流量定向到https。

有人可以帮我了解如何解决吗?

是否需要更改与/ public /文件夹有关的重写规则?

1 个答案:

答案 0 :(得分:0)

http->https文件夹的.htaccess中添加/public_html/public/规则:

<IfModule mod_rewrite.c>

  RewriteCond %{HTTPS} !=on
  RewriteRule (.*) https://%{HTTP_HOST}/$0 [R=301,NE,L]

  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

确保在新的浏览器中进行测试或完全清除浏览器缓存。

相关问题