0

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?

1 Answer 1

0

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]
2
  • 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, mydomain.com/simfiles/mydir1/mydir2 becomes: mydomain.com/simfiles/mydir1/mydir2/?portal=simfiles&folder=mydir1/mydir2 I would like the URL to remain as mydomain.com/simfiles/mydir1/mydir2 Oct 10, 2012 at 3:09
  • Turns out that the issue with urls not being rewritten lies elsewhere. This answer solved my other problem though. Oct 10, 2012 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.