views:

21

answers:

2

i need to rewrite urls for my classified ads directory

i have 4 types of links

/City ==> display all ads in city

/City/Cat1 ==> display all ads in city + category

/City/Cat1/Cat2 ==> display add ads in city + category 1 + category 2

/City/Cat1/Cat2/Ad-id ==> display the ad itself and pass cat1 cat2 and city variables

original hidden url should be index.php?city=alexandria&cat1=cars&cat2=bikes&adid=EWSw22d

Can you please help me writing .htaccess for this structure

+1  A: 

You should probably replace the first rewrite rule for a rewritecond, but this is the idea:

RewriteEngine on
RewriteRule ^index.php index.php [NE,QSA,L]
RewriteRule ^(.*?)(?:/(.*?)(?:/(.*?)(?:/(.*))?)?)?$ index.php?city=$1&cat1=$2&cat2=$3&adid=$4
Artefacto
A: 

I'd make it with single rule to revert all unexisting requests to the index PHP and then parse the request string there. Smth like this

RewriteEngine on
RewriteBase /
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?query=$1 [QSA,L]
Col. Shrapnel