Easy question about mod_rewrite on friendly urls

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

Easy question about mod_rewrite on friendly urls

Postby expertmax1 » Sun Feb 01, 2009 2:01 pm

Hi, i want my page to display :

Code: Select all
http://localhost/hey/ho/


using :

Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&id=$2


The result should be that i can use :

Code: Select all
http://localhost/hey/

and

http://localhost/hey/ho/


Thank you ! Maxime.
expertmax1
 
Posts: 2
Joined: Sun Feb 01, 2009 1:31 pm

Postby richardk » Sun Feb 01, 2009 3:59 pm

Your regular expression only matches /something/something.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Don't match existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Don't match existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [QSA,L]

RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&id=$2 [QSA,L]


Or in one rule
Code: Select all
Options +FollowSymLinks

RewriteEngine On

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

Postby expertmax1 » Mon Feb 02, 2009 3:11 pm

Thank you very much for your help !
expertmax1
 
Posts: 2
Joined: Sun Feb 01, 2009 1:31 pm

Regex Help

Postby aa005 » Thu Apr 16, 2009 5:41 am

aa005
 
Posts: 4
Joined: Thu Apr 16, 2009 5:37 am

Regex Help

Postby aa005 » Thu Apr 16, 2009 5:43 am

aa005
 
Posts: 4
Joined: Thu Apr 16, 2009 5:37 am

Postby richardk » Thu Apr 16, 2009 8:58 am

(?:/([^/]+))? is repeated and nested (?:/([^/]+)here)?
Code: Select all
^([^/]+)(?:/([^/]+)(?:/([^/]+))?)?/?$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby aa005 » Sun Apr 26, 2009 11:54 pm

Hey Richard,
Could you help with regex with 4 levels down as in

http://www.example.com/root/sub/subsub/subsubsub/ as well...Sorry I am not really any good with regexs..

Cheers.
aa005
 
Posts: 4
Joined: Thu Apr 16, 2009 5:37 am

Regex Help

Postby aa005 » Mon Apr 27, 2009 4:05 am

Thanks anyway..I worked it out....


For other's benfit
^([^/]+)(?:/([^/]+)(?:/([^/]+))?(?:/([^/]+))?)?/?$
aa005
 
Posts: 4
Joined: Thu Apr 16, 2009 5:37 am

Postby richardk » Mon Apr 27, 2009 8:45 am

Code: Select all
^([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+))?)?)?/?$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 2 guests

cron