Reg Exp problem

New to mod_rewrite? This is a good place to start.

Reg Exp problem

Postby talawa » Tue Sep 08, 2009 4:13 am

Hi everybody.

First, I want to thank you for this very good forum.
I've searched around old post to figure out my problem, i tried a lot of thinks but i didn't find the solution.
I'm a poor reg exp coder that's why i'm asking you. I want to rewrite urls in this way :
Code: Select all
index.php                                        -> /
index.php?profile                                -> /profile/
index.php?profile=Some+User                      -> /profile/Some User/
index.php?profile=Some+User&show=gallery         -> /profile/Some User/gallery/
index.php?profile=Some+User&show=gallery&ajax    -> /profile/Some User/gallery/ajax/
index.php?inbox                                  -> /inbox/
index.php?inbox=compose                          -> /inbox/compose/
index.php?inbox=read&id=11                       -> /inbox/read/11/
index.php?inbox=read&id=11&ajax                  -> /inbox/read/11/ajax/

Is it possible to have a reg exp that do all those things or do I have to map urls with an external file or script ?

Thank you again. Have a good day.
talawa
 
Posts: 3
Joined: Tue Sep 08, 2009 3:56 am

Postby richardk » Tue Sep 08, 2009 7:58 am

You would need 2 rules because the parameter names (show and id) are different.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^profile(?:/([^/]+)(?:/([^/]+)(?:/(ajax))?)?)?/?$ /index.php?profile=$1&show=$2&$3 [QSA,L]
RewriteRule ^inbox(?:/(compose|read)(?:/([0-9]+)(?:/(ajax))?)?)?/?$ /index.php?inbox=$1&id=$2&$3 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby talawa » Wed Sep 09, 2009 2:14 am

Ok thanks!

I think i will change my php code to be more generic. Is it possible to have just one rule to do a thing like that :
Code: Select all
/                        index.php
/inbox                   index.php?inbox
/inbox/read/11           index.php?inbox=read&show=11
/inbox/read/11/ajax      index.php?inbox=read&show=11&ajax
/profile/user/gallery    index.php?profile=user&show=gallery

Genric url :
/A/B/C/ajax              index.php?A=B&show=C&ajax

where A, B and C can be any characters.

Thank you.
talawa
 
Posts: 3
Joined: Tue Sep 08, 2009 3:56 am

Postby richardk » Wed Sep 09, 2009 12:18 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/(ajax))?)?)?/?$ /index.php?$1=$2&show=$3&$4 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby talawa » Wed Sep 09, 2009 1:32 pm

Thank you very much richardk, it works like a charm!

Have a good day!
talawa
 
Posts: 3
Joined: Tue Sep 08, 2009 3:56 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 39 guests

cron