Newbie needs help with mod rewrite multiple variables

Discuss practical ways rearrange URLs using mod_rewrite.

Newbie needs help with mod rewrite multiple variables

Postby smm18951 » Sun May 11, 2008 11:40 am

I have three strings that I need to rewrite but both need to be done in the same htaccess file.

the first one is:
details.php?merch=173&itm_num=LDUTH0004

and the second one is:
browse.php?merch=173&cat=5

and the third one is:
items.php?merch=173&cat=5&sub=1

I have a function htaccess file now that works great on on string and here it is:
Code: Select all
Options +FollowSymLinks
RewriteEngine on
RewriteRule details-merch-(.*)-itm_num-(.*)\.htm$ details.php?merch=$1&itm_num=$2


As I said I am looking for a way to rewrite all three lines when they are exectued. Anyone have any thoughts
smm18951
 
Posts: 3
Joined: Sun May 11, 2008 11:32 am
Location: online at woocha.com

Postby richardk » Sun May 11, 2008 1:23 pm

Add more rules like the one you already have.

What are the URLs supposed to look like?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby smm18951 » Sun May 11, 2008 1:34 pm

I don't get it...do you mean I can just do this to the file and it will work?

Code: Select all
Options +FollowSymLinks
RewriteEngine on
RewriteRule details-merch-(.*)-itm_num-(.*)\.htm$ details.php?merch=$1&itm_num=$2
RewriteRule browse-merch-(.*)-cat-(.*)\.htm$ browse.php?merch=$1&cat=$2
RewriteRule items-merch-(.*)-cat-(.*)-sub-(.*)\.htm$ items.php?merch=$1&cat=$2&sub=$3


I thought you could only have one rule. Doesn't it matter that the name merch is in all three rules?
smm18951
 
Posts: 3
Joined: Sun May 11, 2008 11:32 am
Location: online at woocha.com

Postby richardk » Sun May 11, 2008 2:45 pm

I thought you could only have one rule.

You can have as many rules as you like.

Doesn't it matter that the name merch is in all three rules?

No, it does not matter as long as the patterns (the first part) are different.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^details-merch-([0-9]+)-itm_num-([0-9a-zA-Z]+)\.htm$  /details.php?merch=$1&itm_num=$2  [QSA,L]
RewriteRule ^browse-merch-([0-9]+)-cat-([0-9]+)\.htm$             /browse.php?merch=$1&cat=$2       [QSA,L]
RewriteRule ^items-merch-([0-9]+)-cat-([0-9]+)-sub-([0-9]+)\.htm$ /items.php?merch=$1&cat=$2&sub=$3 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby smm18951 » Sun May 11, 2008 5:00 pm

Although I appreciate your code example, I could not get it to work at all...is there anything else that I might be able to try?

Thank you
smm18951
 
Posts: 3
Joined: Sun May 11, 2008 11:32 am
Location: online at woocha.com

Postby richardk » Mon May 12, 2008 2:00 pm

What about the rules you posted?

Try
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^details-merch-([0-9]+)-itm_num-([0-9a-zA-Z]+)\.htm$  /details.php?merch=$1&itm_num=$2  [QSA,L]
RewriteRule ^browse-merch-([0-9]+)-cat-([0-9]+)\.htm$             /browse.php?merch=$1&cat=$2       [QSA,L]
RewriteRule ^items-merch-([0-9]+)-cat-([0-9]+)-sub-([0-9]+)\.htm$ /items.php?merch=$1&cat=$2&sub=$3 [QSA,L]


Or
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule details-merch-([0-9]+)-itm_num-([0-9a-zA-Z]+)\.htm$  /details.php?merch=$1&itm_num=$2  [QSA,L]
RewriteRule browse-merch-([0-9]+)-cat-([0-9]+)\.htm$             /browse.php?merch=$1&cat=$2       [QSA,L]
RewriteRule items-merch-([0-9]+)-cat-([0-9]+)-sub-([0-9]+)\.htm$ /items.php?merch=$1&cat=$2&sub=$3 [QSA,L]


Or
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule details-merch-([^/]+)-itm_num-([^/]+)\.htm$       /details.php?merch=$1&itm_num=$2  [QSA,L]
RewriteRule browse-merch-([^/]+)-cat-([^/]+)\.htm$            /browse.php?merch=$1&cat=$2       [QSA,L]
RewriteRule items-merch-([^/]+)-cat-([^/]+)-sub-([^/]+)\.htm$ /items.php?merch=$1&cat=$2&sub=$3 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 48 guests

cron