Apache 多次重写规则 [英] Apache Multiple Rewrite Rules
问题描述
我有两套重写规则.这是虚拟主机:
I have a 2 sets of rewrite rules. This is the Virtual Host:
<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 文件 - 下面是一个示例,但我让网站显示:
I have been trying to add the .htaccess to the Virtual Host so I can remove the .htaccess file - below is an example, but I get the site to show:
<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] 表示要匹配的最后一条规则,所以我删除了它,但它仍然不起作用.
I understand the [L] means last rule to match so I have removed that but it still doesn't work.
我在这里错过了什么?我试过颠倒规则.
What am I missing here? I've tried reversing the rules.
谢谢
推荐答案
L
仍然需要,因为 Last flag 用于标记每个重写规则的结束.规则的排序也很重要.将您的代码更改为:
L
will still be needed as Last flag is for marking end of each rewrite rule. Ordering of rules is also important. Change your code to this:
<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>
这篇关于Apache 多次重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!