【问题标题】:Redirect http to https in Yii2 .htaccess在 Yii2 .htaccess 中将 http 重定向到 https
【发布时间】:2015-10-28 09:36:18
【问题描述】:

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

这是在 index.php 所在的根/前端/web 文件夹中。

RewriteEngine on

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

RewriteRule . index.php

我必须在这里添加 https 吗?

【问题讨论】:

  • 它不会将主页重定向到https ...它停留在http ..它还调用http中的资源...但是http中不允许使用资源..它们只允许在https中.
  • @anubhava 你可以查看iklyn.com
  • 是的,它正在工作......并且因为它产生了问题......可能是两个重写条件相互冲突......!!
  • @anubhava 看到这个iklyn.com url 有效...在这里它将页面重定向到我的前端应用程序..它在其他文件夹中..所以.htaccess 有效...我应用了一些错误规则...两个规则相互冲突
  • @anubhava 是的,它没有重定向到 https ...它只是将资源重定向到 https ..而不是我的 php 文件...

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

我在下面使用了这个代码。

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]

【讨论】:

  • 这只是一个处理 HTTP 和 HTTPS 的 www > 非 www 重定向,而不是 OP 试图做的。
【解决方案2】:

另一种方法是更改​​端口 80 (HTTP) 上的虚拟主机:

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

然后为端口 443 (HTTPS) 设置一个单独的虚拟主机。

假设您是 Debian(-fork) Linux 服务器,您必须将上述内容放在 /etc/apache2/sites-available/myname.conf 中。 完成后使用sudo a2ensite myname.conf(如果失败,您可以自己在/etc/apache2/sites-enabled/ 中创建符号链接,但尽量不要这样做)。现在用 'sudo service apache2 restart' 重启 apache。

【讨论】:

    【解决方案3】:

    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]
    

    请记住,子目录 .htaccess 始终优先于父目录 .htaccess

    【讨论】:

      【解决方案4】:

      当我把它放在根 .htaccess 中时。它也适用于我。

      RewriteEngine on
      
      RewriteCond %{HTTPS} off
      RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
      
      RewriteCond %{REQUEST_URI} !^public
      RewriteRule ^(.*)$ frontend/web/$1 [L] 
      

      【讨论】:

        【解决方案5】:

        最简单的方法可能是在/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();
            }
        },
        ...
        

        【讨论】:

        • 我正在寻找一种 Yii2 方式...很好...!!
        猜你喜欢
        • 2012-11-02
        • 1970-01-01
        • 2019-03-10
        • 1970-01-01
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 1970-01-01
        • 2019-03-18
        相关资源
        最近更新 更多