htaccess not properly rewriting urls

Posted by Cameron Ball on Server Fault See other posts from Server Fault or by Cameron Ball
Published on 2012-10-10T01:50:17Z Indexed on 2012/10/10 3:39 UTC
Read the original article Hit count: 531

Filed under:
|
|

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:/]+)$ http://mydomain.com/?portal=simfiles&folder=$1 [L]

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

mydomain.com/sifmiles/my-files

Will get redirected to

mydomain.com/?portal=simfiles&folder=my-files

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

mydomain.com/sifmiles/my-files/more-of-my-files

Will get redirected to

mydomain.com/?portal=simfiles&folder=my-files/more-of-my-files

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:/\.]+)$ http://mydomain.com/?portal=simfiles&folder=$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:

http://mydomain.com/?portal=simfiles&folder=my-files

I want the URL to remain clean, like:

http://mydomain.com/sifmiles/my-files

How can I achieve this?

© Server Fault or respective owner

Related posts about .htaccess

Related posts about redirect