Ad

Replacement Of Parameters In Seo Url With Htaccess

My url:

x.com/ara?il=istanbul&ilce=avcilar&marka=opel&model=corsa

x.com/rent/istanbul-avcilar-opel-corsa

In htaccess:

RewriteRule ^rent/([0-9a-zA-Z-_]+)-([0-9a-zA-Z-_]+)-([0-9a-zA-Z-_]+)-([0-9a-zA-Z-_]+)$ ara.php?il=$1&ilce=$2&marka=$3&model=$4 [L,QSA]

it works if all parameters exist. But does not work if there is no ilce or model parameter in the url.

So I want it to work also in these situations

x.com/ara?il=istanbul&marka=opel&model=corsa 
x.com/rent/istanbul-opel-corsa

x.com/ara?il=istanbul&marka=opel 
x.com/rent/istanbul-opel

x.com/ara?il=istanbul&ilce=avcilar&marka=opel 
x.com/rent/istanbul-avcilar-opel

How should htaccess be for this?

Ad

Answer

With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##Rule for rewrite of (x.com/rent/istanbul-opel-corsa) TO (x.com/ara?il=istanbul&marka=opel&model=corsa) url.
RewriteRule ^rent/(istanbul)-(opel)-(corsa)/?$ ara?il=$1&marka=$2&model=$3 [NC,L]

##Rule for rewrite of (x.com/rent/istanbul-opel) TO (x.com/ara?il=istanbul&marka=opel) url.
RewriteRule ^rent/(istanbul)-(opel)/?$ ara?il=$1&marka=$2 [NC,L]

##Rule for rewrite of (x.com/rent/istanbul-avcilar-opel) TO (x.com/ara?il=istanbul&ilce=avcilar&marka=opel) url.
RewriteRule ^rent/(istanbul)-(avcilar)-(opel)/?$ ara?il=$1&marka=$2&model=$3 [NC,L]


For Generic rule sets try following:

RewriteEngine ON
##Rule for rewrite of (x.com/rent/istanbul-opel-corsa) TO (x.com/ara?il=istanbul&marka=opel&model=corsa) url.
RewriteRule ^rent/([^-]*)-([^-]*)-(.*)/?$ ara?il=$1&marka=$2&model=$3 [NC,L]

##Rule for rewrite of (x.com/rent/istanbul-opel) TO (x.com/ara?il=istanbul&marka=opel) url.
RewriteRule ^rent/([^-]*)-([^-]*)/?$ ara?il=$1&marka=$2 [NC,L]
Ad
source: stackoverflow.com
Ad