简体   繁体   English

在 Yii2 .htaccess 中将 http 重定向到 https

[英]Redirect http to https in Yii2 .htaccess

htaccess file which redirects my advanced Yii2 app to frontend index.php file for that there is already an .htaccess file. htaccess 文件将我的高级 Yii2 应用程序重定向到前端 index.php 文件,因为已经有一个 .htaccess 文件。 which has following lines..其中有以下几行..

This is Right Now my .HTACCESS at root directory这是现在我在根目录下的 .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]

now i searched internet and i found this if i want to redirect to https then i have to add this.现在我搜索了互联网,如果我想重定向到 https,我发现了这个,那么我必须添加这个。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

so i added like this....所以我加了这样....

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]

then it loads website withouat any js and css... and also it is in http.然后它在没有任何js和css的情况下加载网站......而且它也在http中。 But content it requires should be in https.但它需要的内容应该在 https 中。

I do understand it is because of my another rewrite statement.我确实理解这是因为我的另一个重写声明。 But i am not so expertise in it.但我在这方面不是很专业。 Please mae some suggetion.请提出一些建议。

Here this thing if it is alone it works perfect without any problem.在这里,如果单独使用这个东西,它可以完美运行,没有任何问题。

  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

It will send my http to https.它会将我的 http 发送到 https。 So it works perfact.所以它完美地工作。

Or if this is alone或者如果这是单独的

   RewriteCond %{REQUEST_URI} !^public
   RewriteRule ^(.*)$ frontend/web/$1 [L] 

This rule tells that execute index.php file from the [.htaccess Directory/frontend/web/] directory .此规则告诉从 [.htaccess Directory/frontend/web/] 目录执行 index.php 文件。 Means execute frontend/web/index.php .表示执行 frontend/web/index.php 。 Here it is very important to do this as this is as per the framework structure.这样做非常重要,因为这是根据框架结构进行的。

So i have to combine these two rules together and build a .htaccess which will work for both scenario.所以我必须将这两个规则结合在一起并构建一个适用于这两种情况的 .htaccess。

CONCLUSION结论

So my .HTACESS should tell the server we have to run [.HTACESS DIR/frontend/web/index.php] in https.所以我的 .HTACESS 应该告诉服务器我们必须在 https 中运行 [.HTACES DIR/frontend/web/index.php]。

UPDATE TO QUESTION更新问题

this is the .htaccess under frontend/web/ folder where my index.php is这是我的 index.php 所在的 frontend/web/ 文件夹下的 .htaccess

And this is at root/frontend/web folder where index.php exists.这是在 index.php 所在的 root/frontend/web 文件夹中。

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

Do i have to add https thing here ?我必须在这里添加 https 吗?

The easiest way might be setting the on beforeRequest event handler in /config/web.php like so:最简单的方法可能是在/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();
    }
},
...

You root/frontend/web/.htaccess should be like this:root/frontend/web/.htaccess应该是这样的:

RewriteEngine on
RewriteBase /frontend/web/

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Just remember that a sub-directory .htaccess always takes precedence over parent directory .htaccess .请记住,子目录.htaccess始终优先于父目录.htaccess

Another approach would be to change the virtual host on port 80 (HTTP):另一种方法是更改​​端口 80 (HTTP) 上的虚拟主机:

<VirtualHost *:80>
    ServerName mywebsite.com
    Redirect permanent / https://mywebsite.com
</VirtualHost>

and then have a separate virtual host for port 443 (HTTPS).然后为端口 443 (HTTPS) 设置一个单独的虚拟主机。

Assuming a Debian(-fork) Linux server you'd have to put the above in /etc/apache2/sites-available/myname.conf .假设是 Debian(-fork) Linux 服务器,您必须将上述内容放在/etc/apache2/sites-available/myname.conf After you've done that use sudo a2ensite myname.conf (if this fails, you can create the symlink in /etc/apache2/sites-enabled/ yourself, but try not to).完成后,使用sudo a2ensite myname.conf (如果失败,您可以自己在/etc/apache2/sites-enabled/创建符号链接,但尽量不要)。 Now restart apache with 'sudo service apache2 restart'.现在使用“sudo service apache2 restart”重新启动apache。

In root .htaccess when i put this.当我把这个放在 root .htaccess 中时。 It also works for me.它也适用于我。

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L] 

I've used this code below.我在下面使用了这个代码。

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM