Yii apache配置站点出现400 Bad Request 的解决方法

时间:2021-04-03 19:53:23
<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  DocumentRoot "/www/frogCms/web"
  <Directory "/www/frogCms/web/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require local</Directory>
  
    # 开启 mod_rewrite 用于美化 URL 功能的支持(译注:对应 pretty URL 选项)
    RewriteEngine on
    # 如果请求的是真实存在的文件或目录,直接访问
    RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # 如果请求的不是真实文件或目录,分发请求至 index.php  RewriteRule . index.php # if $showScriptName is false in UrlManager, do not allow accessing URLs with script name RewriteRule ^index.php/ - [L,R=404]
</VirtualHost>

但是这样会出现 400 Bad Request 错误。

解决方法:rewrite规则放到Directory中

<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  DocumentRoot "/www/frogCms/web"
    <Directory "/www/frogCms/web/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require local
    
    # 开启 mod_rewrite 用于美化 URL 功能的支持(译注:对应 pretty URL 选项)
    RewriteEngine on
    # 如果请求的是真实存在的文件或目录,直接访问
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # 如果请求的不是真实文件或目录,分发请求至 index.php
    RewriteRule . index.php

    # if $showScriptName is false in UrlManager, do not allow accessing URLs with script name
    RewriteRule ^index.php/ - [L,R=404]
    
  </Directory>

</VirtualHost>

重启apache,OK