Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite RewriteCond/Rule question

mod_rewrite RewriteCond/Rule question

         

vgjunkie

12:52 am on Mar 18, 2006 (gmt 0)

10+ Year Member



I have a question about mod_rewrite. I am trying to do the following and was wondering if what I am doing is a simple solution:

If a user goes to [site.com...] I would like it to redirect to the following:

/index.php?u=some/string

"some/string" should only be parsed by the rule only if it does not exist on the server. So if "/some/string" was a directory on the server, the rewrite rule would not apply.

I use a multi-tier directory as an example, but it could be just a single level e.g "/some".

Is there something for a RewriteCond that can do a check to see if this is possible?

Here is my current rule(s):

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI}!-f
RewriteCond %{REQUEST_URI}!-d
RewriteCond %{REQUEST_URI}![a-z]+\.[a-z]+
RewriteRule ^(.*)$ /index.php?u=$1 [L,NC]

The only problem is, if I goto say:

[domain.com...]

It will rewrite to index.php?u=images/ even though the /images directory does exist on the server. In other words, if it is not a "filename.ext" it will be rewritten.

I was hoping there would be some sort of check like in perl with the -e flag. I tried that, and it had no effect.

Thanks!

jdMorgan

1:22 am on Mar 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't use the contents of %{REQUEST_URI} as a filename; It will never match. Use

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

instead.

Jim

vgjunkie

1:29 am on Mar 18, 2006 (gmt 0)

10+ Year Member



Ahhh haaa! You rule JD! :)

Final code (for those who might want to use it)

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ /index.php?u=$1 [L,NC]

Thanks for prompt response!

jdMorgan

1:34 am on Mar 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that this forum removes spaces ahead of "!". So here's a "post-final" version (with a few more clean-ups as well):

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?u=$1 [L]

Jim