views:

507

answers:

2

I'm trying to redirect URLs from example.net/customname or example.net/customname/ to example.net/my/home.php?username=customname . This in itself is not complicated:

RewriteRule ^([a-zA-Z0-9_-]+)$ my/home.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ my/home.php?username=$1

However, I want to exclude my existing directories and files, such as example.net/about/ and example.net/files. I can't quite figure out how to use RewriteCond (do I need multiple rewrite conds?) in order to exclude items like example.net/about/ from being rewritten to example.net/home.php?username=about. How can I do this?

Thank you!

EDIT: Here is the exact solution...

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9_-]+)/?$ my/home.php?url=$1 [NC,L]
+2  A: 

You can use a RewriteCond (rewrite condition), similar to this:

RewriteCond %{REQUEST_URI} ! ^\/(about|files)\/?$
RewriteRule ^([a-zA-Z0-9_-]+)/?$ my/home.php?username=$1 [L]

This would effectively only apply the rule if it gets through all the conditions above it, similar to using if statements.

http://httpd.apache.org/docs/2.0/mod/mod%5Frewrite.html#rewritecond

localshred
+3  A: 

RewriteCond has some special patterns for exactly this purpose. Try this:

RewriteCond %REQUEST_FILENAME ! -d
RewriteCond %REQUEST_FILENAME ! -f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ my/home.php?username=$1
David Zaslavsky
I've seen the -d and -f before, but didn't realize that's what they did. Thanks! I posted my full syntactically correct solution above.
Wickethewok