tags:

views:

29

answers:

2

is possible to exclude a url being parsed by mod rewrite? my .htaccess has rewrite rules like

RewriteRule ^contact contact_us.php

and a couple more static pages.

currently my site don't have troubles cause uses http://domain.com/user.php?user=username but now i need rewrite to:

http://domain.com/username

I've tried with:

RewriteRule ^(.*)$ user.php?user=$1 [L]

but all my site stops working...

is possible to avoid parse my static pages like contact/feed/etc being treated like usernames?

edit to match david req:

this is my actual .htaccess file:

RewriteEngine On
Options +Followsymlinks

RewriteRule ^contact contact_us.php [L]
RewriteRule ^terms terms_of_use.php [L]
RewriteRule ^register register.php [L]
RewriteRule ^login login.php [L]
RewriteRule ^logout logout.php [L]
RewriteRule ^posts/(.*)/(.*) viewupdates.php?username=$1&page=$2 
RewriteRule ^post(.*)/([0-9]*)$ viewupdate.php?title=$1&id=$2 
RewriteRule ^(.*)$ profile.php?username=$1 [L] 

also i've enabled modrewrite log my first file:http://pastie.org/1044881

+1  A: 

The -f and -d options to RewriteCond check if the current match is a file or directory on disk.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ....
Ignacio Vazquez-Abrams
thanks for your answer but i got error 500
greenbandit
A: 

Put the rewrite rules for the static pages first, and add the [L] flag to them:

RewriteRule ^contact contact_us.php [L]
...

then after those, use your rewrite rule for the username:

RewriteRule ^(.*)$ user.php?user=$1 [L]

(hopefully nobody has a username of contact).

EDIT: Based on the log output you posted (which I'm assuming corresponds to an unsuccessful attempt to access the contact page... right?), try changing the contact rewrite rule to either

RewriteRule ^contact$ contact_us.php [L]

or

RewriteRule ^contact contact_us.php [L,NS]

That is, either add $ to make the pattern match only the literal URL contact, or add the NS flag to keep it from applying to subrequests. According to the log output, what seems to have happened is that Apache rewrites contact to contact_us.php and then does an internal subrequest for that new URL. So far so good. The weird thing is that the ^contact pattern again matches contact_us.php, "transforming" it to contact_us.php, i.e. the same thing, which Apache interprets as a signal that it should ignore the rule entirely. Now, I would think Apache would have the sense to ignore the rule only on the subrequest, but I'm not sure if it's ignoring the entire rewriting process and leaving the original URL, /contact, as is. If that's the case, making one of the changes I suggested should fix it.

EDIT 2: your rewrite log excerpt reminded me of something: I'd suggest making the rewrite rule

RewriteRule ^([^/]+)$ user.php?user=$1 [L]

since slashes shouldn't be occurring in any usernames. (Right?) Or you could do

RewriteRule ^(\w+)$ user.php?user=$1 [L]

if usernames can only include word characters (letters, numbers, and underscore). Basically, make a regular expression that matches only any sequence of characters that could be a valid username, but doesn't match URLs of images or CSS/JS files.

David Zaslavsky
thanks for the answer but all pages now are parsed like user.phpthis code works RewriteRule ^user/(.*)$ user.php?user=$1 [L]but isn't base of site.any idea to tell rewriterule don't parse previously listed static pages?
greenbandit
So you're saying that when you access `example.com/contact`, the `RewriteRule ^contact ...` _doesn't_ seem to apply? If so, something must be wrong with the way your rules are configured. It would help if you edit your question to contain the _entire_ contents of the `.htaccess` file (or at least all the rewriting rules in it), along with its location in the filesystem, and the location of the document root. Also (maybe better yet), can you enable the [rewrite log](http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog)?
David Zaslavsky
i've updated content and posted log. thanks again
greenbandit
works like a charm! thanks.
greenbandit
Cool :-) You'll have to do the same with `terms`, `register`, `login`, and `logout` of course. Also, if my answer worked for you, it'd be nice if you mark it as accepted.
David Zaslavsky