deimos
deimos

Reputation: 55

.htaccess rewrite rule ignoring string, multiple GET variables in url

I am trying to make .htaccess rewrite rule to map 4 different get variables and exclude one string. String is unchangeable ie. always will remain same.

Current url is:

/car.php?make=bmw&model=z4&year=2006&color=black_metallic

It should be like this:

car/bmw-z4-2006-black_metallic-for-sale

This is I've done so far

RewriteRule ^car/^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ car.php?make=$1&model=$2&year=$3&color=$4

Now I need to ignore string -for-sale at the end of the pretty url.

Any help greatly appreciated!

Upvotes: 3

Views: 154

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

Try this

 RewriteRule ^car/([^/-]+)-([^/-]+)-([^/-]+)-([^/-]+)/?$ car.php?make=$1&model=$2&year=$3&color=$4 [QSA,NC,L]

Upvotes: 2

anubhava
anubhava

Reputation: 785226

Your delimiter is hyphen not forward slash hence your RewriteRule should also handle that accordingly:

Options -MultiViews
RewriteEngine On

RewriteRule ^car/^([^-]+)-([^-]+)-([^-]+)-([^-]+) car.php?make=$1&model=$2&year=$3&color=$4 [L,QSA,NC]
  • Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

Upvotes: 1

Related Questions