htaccess 文件,它将我的高级 Yii2 应用程序重定向到前端 index.php 文件,因为已经有一个 .htaccess 文件。其中有以下几行..
这是我现在在根目录下的 .HTACCESS
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
</IfModule>
# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>
# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]
现在我搜索了互联网,我发现如果我想重定向到 https,那么我必须添加它。
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
所以我这样添加....
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
</IfModule>
# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>
# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]
然后它会加载没有任何 js 和 css 的网站...而且它是在 http 中。但是它需要的内容应该是https。
我明白这是因为我的另一个重写语句。但我不是很专业。请给我一些建议。
这里这个东西如果单独使用它可以完美地工作,没有任何问题。
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
它会将我的 http 发送到 https。所以它完美地工作。
或者如果这是单独的
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
这条规则告诉我们从 [.htaccess Directory/frontend/web/] 目录执行 index.php 文件。表示执行 frontend/web/index.php 。这样做非常重要,因为这是按照框架结构进行的。
所以我必须将这两个规则结合在一起并构建一个适用于这两种情况的 .htaccess。
结论
所以我的 .HTACESS 应该告诉服务器我们必须在 https 中运行 [.HTACESS DIR/frontend/web/index.php]。
问题更新
这是我的index.php所在的frontend/web/文件夹下的.htaccess
这是 root/frontend/web 文件夹,index.php 所在的文件夹。
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
我必须在这里添加 https 吗?
最佳答案
最简单的方法可能是在 /config/web.php
中设置 on beforeRequest
事件处理程序,如下所示:
...
'bootstrap' => ['log'],
'on beforeRequest' => function ($event) {
if(!Yii::$app->request->isSecureConnection){
$url = Yii::$app->request->getAbsoluteUrl();
$url = str_replace('http:', 'https:', $url);
Yii::$app->getResponse()->redirect($url);
Yii::$app->end();
}
},
...
关于php - 在 Yii2 .htaccess 中将 http 重定向到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31835406/