开发者

How to use multiple RewriteRule rules in htaccess?

开发者 https://www.devze.com 2023-04-08 05:55 出处:网络
I\'m using 2 RewriteRule rules in my htaccess RewriteEngine on RewriteBase / # if a directory o开发者_StackOverflow中文版r a file exists, use it directly

I'm using 2 RewriteRule rules in my htaccess

RewriteEngine on
RewriteBase /
# if a directory o开发者_StackOverflow中文版r 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]

However this doesn't work, when I'm trying in my local machine, my Apache always use the second rule, and in my hosting server Apache always display an error.

What am I doing wrong ?


What's happening is the 2nd time around (after the URI has been rewritten to /photo_render.php) the 2nd rule is being applied, thus /photo_render.php is getting rewritten to index.php. You need to add a few conditions when you try to match something like . or .*. This should work:

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]

Note that this will also fail if for some reason you don't have photo_render.php.

0

精彩评论

暂无评论...
验证码 换一张
取 消