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

This is a bit of a weird one. I'm doing some work on a server, and I need rewrite rules for directories that actually exist (in some cases, they are more than one level deep)

At the moment my .htaccess looks like this:

RewriteEngine on
RewriteRule ^simfiles/([-\ a-zA-Z0-9:/]+)$$1 [L]

And this is working OK, for example, a url like:

Will get redirected to

Or in the case of a directory structure that is deeper than one level:

Will get redirected to

I wrote the regex so that it won't match things with a . in the path, because there are css and js files which reside in simfiles/somedirectory, and if I redirect everything then these cannot be loaded.

I tried a configuration like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^simfiles/([-\ a-zA-Z0-9:/\.]+)$$1 [L]

But that doesn't work, things still don't load properly.

So my first question is, how can I achieve this "properly"? I don't like my solution because it means redirects won't occur if the folder has a . in its name.

My second problem, is that while the redirection is happening properly, the url becomes:

I want the URL to remain clean, like:

How can I achieve this?

share|improve this question
up vote 0 down vote accepted

You're overthinking your regex. Since you're already checking whether the underlying file exists before the rewrite rule, you should be able to just match anything.

RewriteRule ^simfiles/(.+) /?portal=simfiles&folder=$1 [L,QSA]
share|improve this answer
This rule seems to do what I want. Except the issue with the 'nice url' not persisting remains. Using RewriteCond %{REQUEST_FILENAME} !-f and your rule, becomes: I would like the URL to remain as – Cameron Ball Oct 10 '12 at 3:09
Turns out that the issue with urls not being rewritten lies elsewhere. This answer solved my other problem though. – Cameron Ball Oct 10 '12 at 4:53

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.