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 need a mod_rewrite rule that sends all requests to index.php unless the request is for anything in the /media/ folder.

I have a similar rule for lighttpd but I don't know enough about regex/mod_rewrite to change it for Apache.

        url.rewrite-once = (
            "/(media)/(.*)" => "/$1/$2",
            "^([^\?]*)(\?(.+))?$" => "/index.php?url=$1&$3",


share|improve this question
up vote 3 down vote accepted

Making some corrections to Redmumba's answer...

RewriteEngine on
RewriteMap esc int:escape
RewriteCond $0 !^/media/
RewriteRule .* /index.php?url=$0\%3f${esc:%{QUERY_STRING}} [B]

Use the $0 backreference rather than %{PATH_INFO} to refer to the input path. In the RewriteRule, add the B flag to escape the URL again (as explained in the mod_rewrite docs). The rest of the rule is there to double-escape the original query string, so that it can be recovered when the url parameter is unescaped by your index.php.

Alternatively, if your index.php expects to use its query string directly rather than decoding it from within the url parameter, you would use

RewriteEngine on
RewriteCond $0 !^/media/
RewriteRule .* /index.php?url=$0 [B,QSA]

This is actually a more faithful translation of your LighTTPD rewrite rule, but you run the risk of a conflict in case the input query string contains a url parameter itself.

share|improve this answer
+1 for the query escaping; I didn't even think of that! – Andrew M. Jan 21 '11 at 2:49

Basically, you're going to be doing the same exact things, only calling RewriteRule instead of url.rewrite-once. Also, one improvement that could be made to both is to use a RewriteCond to match the media part.

RewriteEngine on
RewriteCond %{PATH_INFO} !^/media/.*
RewriteRule ^([^\?]*)(\?(.+))?$ /index.php?url=$1&$3

This way, you're not having to rewrite the rule for /media--this checks to see if it doesn't begin with /media and rewrites it. If it does, it just passes it along.

Hope this helps!

share|improve this answer

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.