简体   繁体   中英

How to use multiple RewriteRule rules in htaccess?

I'm using 2 RewriteRule rules in my htaccess

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM