DimitrisD
DimitrisD

Reputation: 424

mod_rewrite rewriting base

i'm working on a website, where i'd like to use mod_rewrite for rewriting URLs like

www.example.com/index.php?p=MYPAGE --> www.example.com/MYPAGE www.example.com/index.php?p=user?u=MYUSER --> www.example.com/user/MYUSER www.example.com/index.php?p=group?g=MYGROUP --> www.example.com/group/MYGROUP

I've added so far these rules.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?p=$1
RewriteRule ^user/(.+)$ index.php?p=user&u=$1

but i'm facing a problem. if i visit www.example.com/single-page everything is fine. Then if i visit www.example.com/user/SMTHING, i'm not getting to what i want. I've tried using C and L flag, without any success.

Any ideas?

Thank you

Upvotes: 1

Views: 146

Answers (4)

Olivier Pons
Olivier Pons

Reputation: 15778

No answer may look like mine, so here it is:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([a-zA-Z0-9-]+)$ /index.php?p=user&u=$1 [QSA,L]
RewriteRule ^group/([a-zA-Z0-9-]+)$ /index.php?p=group&g=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9-]+)$ /index.php?p=$1 [QSA,L]

I'm pretty sure it works (or very close to what you want). Please tell me if it works...

By the way how about something nicer like:

RewriteRule ^(user|group)/([a-zA-Z0-9-]+)$ /index.php?type=$1&val=$2 [QSA,L]

so in your GET you will have "type=user" or "type=group" and "val=the value". Kind of generalize stuff.

Upvotes: 2

GRaecuS
GRaecuS

Reputation: 236

try this, it will work:

RewriteEngine On

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

RewriteRule ^([^\/]+)$ index.php?p=$1
RewriteRule ^([^\/])([^\/]+)\/([^\.]+)$ index.php?p=$1$2&$1=$3

Upvotes: 0

Vishal Pawar
Vishal Pawar

Reputation: 4340

will you try this it may be useful.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(.+)$ index.php?p=user&u=$1
RewriteRule ^(.+)$ index.php?p=$1

The order of Rewrite rules is very important.

Upvotes: 0

Conspicuous Compiler
Conspicuous Compiler

Reputation: 6469

You have three issues. The first is that your conditions only apply to the first rule that follows them, so you'll still rewrite legitimate file requests. Further, you need to use the [L]ast flag as well as re-order your rules like this. Putting it all together:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+)$ $1                               [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ $1                               [L]

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

Upvotes: 2

Related Questions