Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Yii2.htaccess中将http重定向到https_Php_Regex_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

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

Php 在Yii2.htaccess中将http重定向到https,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,htaccess文件,它将我的高级Yii2应用程序重定向到frontend index.php文件,因为已经有一个.htaccess文件。有以下几行 这是我现在在根目录下的.HTACCESS Options -Indexes <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_URI} !^public RewriteRule ^(.*)$ frontend/web/$1 [L] </

htaccess文件,它将我的高级Yii2应用程序重定向到frontend 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]
所以我加了这样一句

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]
它会将我的http发送到https。因此,它的性能非常好。

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

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

RewriteRule . index.php
或者如果这是单独的

   RewriteCond %{REQUEST_URI} !^public
   RewriteRule ^(.*)$ frontend/web/$1 [L] 
此规则告诉您从[.htaccess目录/frontend/web/]目录执行index.php文件。表示执行frontend/web/index.php。这一点非常重要,因为这是根据框架结构进行的。

因此,我必须将这两个规则结合起来,构建一个适用于这两种情况的.htaccess

结论

因此my.HTACESS应该告诉服务器我们必须以https运行[.HTACESS DIR/frontend/web/index.php]

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

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

RewriteRule . index.php
问题更新

这是我的index.php所在的frontend/web/文件夹下的.htaccess

这是index.php所在的root/frontend/web文件夹。

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

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

RewriteRule . index.php

我必须在这里添加https吗?

我在下面使用了这段代码

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]

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


服务器名mywebsite.com
重定向永久/https://mywebsite.com
然后为端口443(HTTPS)提供一个单独的虚拟主机

假设使用Debian(-fork)Linux服务器,则必须将上述内容放入
/etc/apache2/sites available/myname.conf
中。
完成此操作后,请使用
sudoa2ensite myname.conf
(如果失败,您可以自己在
/etc/apache2/sites enabled/
中创建符号链接,但不要尝试)。现在使用“sudo service apache2 restart”重新启动apache。

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
始终优先于root.htaccess中的父目录
.htaccess

。它对我也有用

RewriteEngine on

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

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

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

它不会将主页重定向到https。。。它保持在http中。。它还需要http中的资源。。。但是http中不允许使用资源。。它们只允许在https中使用。@anubhava您可以检查是,它正在工作。。。正因为如此,才产生了问题。。可能是两个重写条件相互冲突@阿努巴瓦看到这个网址了吗。。。在这里,它将页面重定向到我的前端应用程序。它位于其他文件夹中。。所以,htaccess是有效的。。。我应用了一些错误的规则。。。这两条规则相互冲突other@anubhava是的,它没有重定向到https。。。它只是将资源重定向到https。。不是我的php文件…这只是一个www>非www重定向,处理HTTP和HTTPS,而不是OP试图做的。我正在寻找一种Yi2方法。。。太好了。。。!!