2 variables

Discuss practical ways rearrange URLs using mod_rewrite.

2 variables

Postby deamon » Fri May 23, 2008 1:51 pm

Hi, I'm trying to get 2 variables to form a URL:

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^/([a-z-]{2})/([a-z]+)$ /index.php?lang=$1&p=$2[QSA,L]


It gives me a 404 error. mod rewrite is enabled and working. As you can see, I'm trying to get the language chosen and the page too:

/index.php?lang=en&p=home

to

/en/home



Thanks.

Edit: Yes the extra dash in the first sub pattern is there for a reason. (Any letter and dashes are allowed.)




Edit 2: Nevermind I got it working. Here's the fixed version:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([a-z]{2})/([a-z-]+)$ /index.php?lang=$1&p=$2 [QSA,L]
deamon
 
Posts: 16
Joined: Thu Jul 20, 2006 9:13 pm

similar problem of 2 variables

Postby pharaon1984 » Mon Mar 02, 2009 4:22 pm

Hello,

I've similar problem with 2 variables.

I have categories and items for every category.

So, I need to transform from:

mydomain.com/index.php?module=sections&cat_id=15
mydomain.com/index.php?module=sections&cat_id=15&item_id=32

to

mydomain.com/custom_title/15/
mydomain.com/custom_title/15/32/

I put in htaccess the code:

Code: Select all
RewriteRule ^(.+)/(.+)/([0-9]+)/$ index.php?module=sections&cat_id=$3 [L]
RewriteRule ^(.+)/(.+)/([0-9]+)/([0-9]+)/$ index.php?module=sections&cat_id=$3&item_id=$4 [L]


The first friendly URL is working. Second one opens me a blank page, so it seems that the variables are not send properly.

Anybody knows the solution?

Thank you.
pharaon1984
 
Posts: 1
Joined: Mon Mar 02, 2009 4:14 pm

Postby richardk » Tue Mar 03, 2009 3:11 am

Why have you got (.+)/(.+)?

The first rule matches both URLs, try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^.+/([0-9]+)/([0-9]+)/?$ /index.php?module=sections&cat_id=$1&item_id=$2 [QSA,L]
RewriteRule ^.+/([0-9]+)/?$         /index.php?module=sections&cat_id=$1 [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 19 guests

cron