alexserver
alexserver

Reputation: 1358

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 ?

Upvotes: 0

Views: 496

Answers (1)

Jon Lin
Jon Lin

Reputation: 143856

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.

Upvotes: 1

Related Questions