htaccess not properly rewriting urls

Cameron Ball asked:

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?

My answer:

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]

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.