Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to mod_rewrite. My problem: I have two rules colliding with each other.

In my doc root I used to have only one .htaccess redirecting all incoming requests to a fast-cgi script like so:

RewriteRule ^(.*)$ fcgi-bin/python-script.fcgi/$1 [QSA,L]

Now I have a folder with php scripts in my doc root let's call it "specialdir". How can I redirect requests from to the php specialfolder while having the rest of the requests still sent to the fcgi script?

I tried

RewriteRule ^special$ specialfolder/$1 [QSA,L]

But the request is obviously still handled by my "catch all" ^(.*)$ directive.

Should I include some kind of condition? How would it have to look?

EDIT: My full .htaccess file

AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

AuthType Basic
AuthName "login"
AuthUserFile /var/www/v/htuser
Require valid-user

#php rewrite
RewriteRule ^special/(.*) specialfolder/$1 [QSA,L]

#trac rewrite
RewriteRule ^trac(.*)$ fcgi-bin/trac-proj/$1 [QSA,L]

#django rewrite
RewriteRule ^(.*)$ fcgi-bin/django/$1 [QSA,L]
RewriteRule ^media/(.*)$ fcgi-bin/django/media/$1 [QSA,L]
share|improve this question

Rewrite rules are applied in the order they are listed in the .htaccess file. If you move the second rule up above the first rule, it will take precedence.

However, the second rule has other problems. By "bookending" your regex with ^ and $ you will only match special exactly, special/ will not be matched and neither will special/foobar.

Also, the replacement part contains $1 which will be replaced by whatever was matched inside the parentheses. Since you have no parentheses in your pattern, the $1 in the replacement is confusing.

What you probably want is this:

RewriteRule ^special/(.*) specialfolder/$1 [QSA,L]

And make sure it is above the previously mentioned rewrite in the file.

share|improve this answer
THX but this produces the same result. The rule first mentioned still takes precedence. Even though placed behind. – j7nn7k Nov 29 '11 at 15:48
Could you add the entire current .htaccess to your question? – Ladadadada Nov 29 '11 at 16:02
Added the whole file. – j7nn7k Nov 29 '11 at 16:08
Ah, I see the problem. The RewriteCond at the very top applies to the next RewriteRule, no matter how far down the file it may be. If the URI starting with special/ actually matches a file in the special/ directory then the rewrite that makes it work will be ignored. Either put the RewriteCond in the correct place or comment it out. – Ladadadada Nov 29 '11 at 16:19
Couldn't make it work... I moved the "special" folder to another location and created a subdomain. in this case it was the cleaner solution. thx for your help anyways! – j7nn7k Nov 29 '11 at 16:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.