views:

216

answers:

2

I'm using mod_rewrite in a ManagedFusion settings file (similar to .htaccess) and I want to forward all incoming requests to another port on the server, except for a few folders.

So far I have this (with the folder ui, forms, clientsystem and widgets being ignored)

RewriteEngine On  

RewriteCond %{REQUEST_URI} !ui$
RewriteCond %{REQUEST_URI} !forms$
RewriteCond %{REQUEST_URI} !clientsystem$
RewriteCond %{REQUEST_URI} !widgets$
RewriteRule ^/(.*) http://localhost:8050/$1 [P,L]

This works fine as long as those exception directories don't exist, however when they are created, the rewriteengine will simply trigger again and in that case it will match the rule, thereby forwarding me to the other port after all.

How could I solve this?

Another question, if I want queries like '?file=bla' to be forwarded as well, will this simply work like this?

Edit: this is what I ended up with:

RewriteCond %{QUERY_STRING} ^(.+)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^ui
RewriteCond $1 !^forms
RewriteCond $1 !^clientsystem
RewriteCond $1 !^widgets
RewriteRule ^/?(.*) http://localhost:8050/$1?%1 [P,L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^ui
RewriteCond $1 !^forms
RewriteCond $1 !^clientsystem
RewriteCond $1 !^widgets
RewriteRule ^/?(.*) http://localhost:8050/$1 [P,R]

Some redundancy but it works.

+1  A: 

Try adding this to only apply the rule if the file or directory by that name doesn't exist:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Btw, I find this mod_rewrite cheat sheet pretty handy: http://dreev.es/modrewrite.

dreeves
+1  A: 

As for second part of your question, make RewriteRule flags to be like: [P,L,QSA]. QSA stands for Query String Append, and will do the obvious to your final string before redirection occurs.

By the way, you would most likely want to include R in flags, since it will truly redirect client to new URL.

Update: so now I understand that you want to skip those directories in any case?

If that's so, then you could use

RewriteCond $1 !^ui
RewriteCond $1 !^forms
...
RewriteRule ^/(.*) http://localhost:8050/$1 [P,L,QSA]

which would simply not match and not forward user in case requested URL starts with any of given strings.

mr.b
Thanks, yes you're right I don't want to forward in any case. His solution also seemed to work though, I'll try yours as well.
Bas van den Broek
I tried the QSA flag but I'm not sure if it works. Is there a way to test this easily? I went to http://localhost:8040/test/?tempquery and I turned logging on with RewriteLog "rewrite.log" and RewriteLogLevel 9, in the log file it says: 2010-06-02T14:41:58 [Rewrite] Proxy: http://localhost:8050/test/Is that expected?
Bas van den Broek
You should see your full URL, along with query string part in your logs on line such as 127.0.0.1 - - [02/Jun/2010:15:13:58 +0200] [localhost/sid#.../initial] (1) [perdir C:/www/root/] redirect to http://localhost/index.php?asdf [REDIRECT/302]. Honestly, I'm not sure how mod_rewrite handles internal redirect to another host. So, in my experience, I would have used R,QSA flags there and would have no problem with it.
mr.b
mr.b
I decided to skip the QSA thing as it did not work for me, see my edited post for what I ended up with. Thanks for the help :)
Bas van den Broek