combine rules

Discuss practical ways rearrange URLs using mod_rewrite.

combine rules

Postby Xtnct00WS6 » Wed Oct 15, 2008 5:37 pm

Basically...I have two rules I'm trying to combine:

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

The first rule works in rewriting this "http://www.index.php?location=books&id=33422" to "http://www.test.com/books/33422"

The second rule works in rewriting "http://www.test.com/index.php?location=main_book_section" to "http://www.test.com/main_book_section"

How can I combine them both??
Xtnct00WS6
 
Posts: 4
Joined: Wed Oct 15, 2008 5:28 pm

Postby richardk » Thu Oct 16, 2008 6:44 am

Yes
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^/]+)(?:/([0-9]+))?/?$ /index.php?location=$1&id=$2 [QSA,L]

Anything between (?: and )? is optional.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Xtnct00WS6 » Thu Oct 16, 2008 8:49 am

Nice!! Thanks. I have a weird issue with that idea of the (?: )?

It works locally on my windows xp machine, but not on the virtual linux server. Any idea why?

This is what I have working locally:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/\.]+)(?:/?([^/\.]+))?/?$ index.php?location=$1&id=$2 [QSA,L]

I also have this working locally but not on the virtual server:

#RewriteRule ^/?([^/\.]+)/?$ index.php?location=$1 [QSA,L]
#RewriteRule ^books/([0-9]+)$ index.php?location=books&id=$1 [QSA,L]
#RewriteRule ^sports/([0-9]+)$ index.php?location=sports&id=$1 [QSA,L]
#RewriteRule ^furniture/([0-9]+)$ index.php?location=furniture&id=$1 [QSA,L]

virtual server: http://dev.abc01.store_front.net:9000/store/
localhost: http://localhost/store/

Thanks for the help!
Xtnct00WS6
 
Posts: 4
Joined: Wed Oct 15, 2008 5:28 pm

Postby Xtnct00WS6 » Thu Oct 16, 2008 9:07 am

I should add that the only part not working is the 2 level deep section (when there is an id). I can get the 1 level deep rule to work fine though.
Xtnct00WS6
 
Posts: 4
Joined: Wed Oct 15, 2008 5:28 pm

Postby Xtnct00WS6 » Thu Oct 16, 2008 10:29 am

I got it...htaccess permission was turned off.

This worked great:

RewriteRule ^([^/\.]+)(?:/?([^/\.]+))?/?$ index.php?location=$1&id=$2 [QSA,L]
Xtnct00WS6
 
Posts: 4
Joined: Wed Oct 15, 2008 5:28 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 8 guests

cron