Apache rewrite 出现 400 Bad Request 的解决方法

时间:2021-04-03 19:52:59

<VirtualHost *:80 *:81>    
    ServerAdmin deng5765@163.com    
    DocumentRoot /active/www/yingchao123/trade    
    ServerName trade.test.yingchao123.com
    ServerAlias trade.yingchao123.com
    
    RewriteEngine On
    RewriteRule ^([\da-z]{1,10})$ ./index.php?mod=list&do=unique&alias=$1 [L]    
    RewriteRule ^([\da-z]{1,10})&(.+)$ ./index.php?mod=list&do=unique&alias=$1&$2 [L]
</VirtualHost>

但这样写会出现 出现 400 Bad Request 错误

解决方法是将rewrite规则写倒directory中:

<VirtualHost *:80 *:81>    
    ServerAdmin deng5765@163.com    
    DocumentRoot /active/www/yingchao123/trade    
    ServerName trade.test.yingchao123.com
    ServerAlias trade.yingchao123.com

    <Directory "/active/www/yingchao123/trade">
        RewriteEngine On
        RewriteRule ^([\da-z]{1,10})$ ./index.php?mod=list&do=unique&alias=$1 [L]        
        RewriteRule ^([\da-z]{1,10})&(.+)$ ./index.php?mod=list&do=unique&alias=$1&$2 [L]
    </Directory>
</VirtualHost>

重启Apache,问题解决

 说明:apache检查配置是否语法错误httpd.exe -t 或者apachectl -t,nginx对应的命令是nginx -t