Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 www.mydomain.com/special 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

1 Answer 1

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
1  
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

 
discard

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.