Bhavesh G
Bhavesh G

Reputation: 3028

Apache URL Rewriting issue

i've set the rules,

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?userid=$1

it redirects http://localhost/username to http://localhost/user.php?id=username works fine, but for other existing folder page still redirects to user.php page; not that existing folder, ex http://localhost/folder it still redirects to http://localhost/folder/?userid=folder, how can it make it to work for existing directories, ex when i type http://localhost/folder then page should shown of /folder/ directory ???

Upvotes: 2

Views: 155

Answers (2)

anubhava
anubhava

Reputation: 784888

Replace your .htaccess code with this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

RewriteRule ^(messages)/?$ index.php?page=$1 [L,NC,QSA]

RewriteRule ^(A-Z0-9_-]+)/?$ user.php?userid=$1 [L,NC,QSA]

If it still doesn't work please post matching long entries from access.log and enable RewriteLog in your httpd.conf and post logs from there in your question.

Upvotes: 1

Ansari
Ansari

Reputation: 8218

The problem is that your second RewriteRule has no RewriteConds. Your two RewriteConds only apply for the first rule, then they stop. The second rule therefore applies unconditionally to every request URI. So this part of your .htaccess should look like this instead:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^messages/?$ index.php?page=messages [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?userid=$1 [L,NC,QSA] 

Upvotes: 1

Related Questions