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'm having trouble with this rewrite. I've never really used mod_rewrite before and don't have much experience with regex. Any help is appreciated!

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
#prevent nested looping
#re-route incoming requests
RewriteRule ^(.*)$ %{REQUEST_URI}2009/$1 [L,NE]

This partially works, is routed to but a request like fails. I'm hoping to rewrite all requests to the parent directory into the 2009 subdirectory but I keep encountering infinite loops and server errors messages. I haven't found any useful examples out there. I figured this would be a common rewrite...

Thanks in advance!

share|improve this question

migrated from Jun 5 '10 at 6:15

This question came from our site for professional and enthusiast programmers.

Oh, and Casey: should /dir/css/theme.css be rewritten to /dir/2009/css/theme.css or /dir/css/2009/theme.css? – David Z Jun 4 '10 at 23:52
up vote 1 down vote accepted

There are few problems with the rewrite rule in your example. First of all, it doesn't really do what you want - you don't need %{REQUEST_URI} bit. With the original path like /dir/css/theme.css REQUEST_URI will contain /dir/css/theme.css and $1 will contain css/theme.css (matched by (.*)) resulting with final URL being /dir/css/theme.css2009/css/theme.css. So we need to get rid of the %{REQUEST_URI} part which leaves us with following RewriteRule

^(.*)$ 2009/$1 [L,NE]

that will translate /dir/css/theme.css into /dir/2009/css/theme.css.

However there's still one problem with that rule as it will cause infinite nesting since (.*) will match any URL. So what happens is that in the first step our example URL gets rewritten to /dir/2009/css/theme.css. And since the new URL /dir/2009/css/theme.css matches our rule too it will get rewritten again, and then again and again...

So basically we need something that will prevent rewriting URLS that were already rewritten. So if URL already contains /dir/2009 we want Apache to leave it alone. One way to accomplish that is to add following condition to the rewrite rule

RewriteCond %{REQUEST_URI} ^(?!/dir/2009).*$

with final .htaccess file like this

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /dir
    RewriteCond %{REQUEST_URI} ^(?!/dir/2009).*$
    #re-route incoming requests
    RewriteRule ^(.*)$ 2009/$1 [L,NE]

Another way to do it is to create .htacess file in dir/2009 directory which will prevent Apache using the one from the parent directory with something like RewriteEngine off. Or perhaps RewriteEngine on without any rules should do the trick as well.

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.