Apache 多重重写规则
Posted
技术标签:
【中文标题】Apache 多重重写规则【英文标题】:Apache Multiple Rewrite Rules 【发布时间】:2013-06-13 03:19:20 【问题描述】:我有两套重写规则。这是虚拟主机:
<VirtualHost *:80>
ServerName datingjapan.co
ServerAlias *.datingjapan.co
RewriteEngine on
RewriteCond %HTTP_HOST !^www\. [NC]
RewriteRule ^(.*) http://www.%HTTP_HOST$1 [R=301,L]
DocumentRoot /var/www/html/datingjapan.co
</VirtualHost>
这是.htacess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-l
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
我一直在尝试将 .htaccess 添加到虚拟主机,以便我可以删除 .htaccess 文件 - 下面是一个示例,但我让网站显示:
<VirtualHost *:80>
ServerName datingjapan.co
ServerAlias *.datingjapan.co
RewriteEngine on
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-l
RewriteRule ^(.*)$ index.php?/$1
RewriteCond %HTTP_HOST !^www\. [NC]
RewriteRule ^(.*) http://www.%HTTP_HOST$1 [R=301,L]
DocumentRoot /var/www/html/datingjapan.co
</VirtualHost>
我知道 [L] 表示要匹配的最后一条规则,因此我已将其删除,但它仍然不起作用。
我在这里缺少什么?我试过颠倒规则。
谢谢
【问题讨论】:
【参考方案1】:L
仍然需要,因为 Last 标志用于标记每个重写规则的结束。规则的顺序也很重要。将您的代码更改为:
<VirtualHost *:80>
ServerName datingjapan.co
ServerAlias *.datingjapan.co
DocumentRoot /var/www/html/datingjapan.co
RewriteEngine on
RewriteCond %HTTP_HOST !^www\. [NC]
RewriteRule ^(.*) http://www.%HTTP_HOST$1 [R=301,L]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-l
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]
</VirtualHost>
【讨论】:
【参考方案2】:.htaccess 主体上需要有 2 个 [L] 标志。
RewriteEngine On
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-f
#for the purpose of multiple rewrite rules
RewriteRule ^(.*)$ index.php/$1 [L]
#for the purpose of multiple rewrite rules (usage of multiple L flag)
RewriteCond %HTTPS off
RewriteRule ^ https://%HTTP_HOST%REQUEST_URI [R,L]
【讨论】:
以上是关于Apache 多重重写规则的主要内容,如果未能解决你的问题,请参考以下文章