如何在htaccess中使用多个RewriteRule规则?

时间:2011-09-28 22:13:46

标签: apache .htaccess mod-rewrite

我在htaccess中使用了2个RewriteRule规则

RewriteEngine on
RewriteBase /
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#if it has member/photo/render_fast forward to photo_render.php
RewriteRule ^member/photo/render_fast(.*) /photo_render.php?r=$1 [L]
# otherwise forward it to index.php
RewriteRule . index.php [L]

然而这不起作用,当我在我的本地机器上尝试时,我的Apache总是使用第二条规则,而在我的托管服务器中,Apache总是显示错误。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

正在发生的事情是第二次(在URI被重写为/photo_render.php之后)正在应用第二条规则,因此/photo_render.php被重写为index.php。当您尝试匹配..*之类的内容时,您需要添加一些条件。这应该有效:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^member/photo/render_fast(.*) /photo_render.php?r=$1 [L]
# otherwise forward it to index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

请注意,如果由于某种原因您没有photo_render.php,这也会失败。

相关问题