PatrikWWDC
PatrikWWDC

Reputation: 83

How to rewrite url that uses get variables with .htaccess

I'm trying to write a .htaccess file that would enable me to write

sampleurl.com/car.php?car=1&name=Audi-A4

And than it would make the url:

sampleurl.com/cars/Audi-A4/1

my current code is:

RewriteEngine on
RewriteRule ^cars/([0-9][0-9])$ /cars/$1/ [R]
RewriteRule ^cars/([0-9][0-9])$ /cars/$1/ [R]
RewriteRule ^cars/([0-9][0-9])/$ /car.php?car=$1

Are there any ideas how could I make this work?

EDIT:

Both codes work:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /car.php?name=$1&car=$2 [L]

and

Options -MultiViews
RewriteEngine On

RewriteRule ^cars/([\w-]+)/(\d+)/?$ car.php?car=$2&name=$1 [L,QSA,NC]

but when I open the website, it shows just the page, without linked css file, how can I link the css file to that webpage?

Current CSS Link code:

<link rel="stylesheet" type="text/css" href="style/main.css">

Upvotes: 3

Views: 79

Answers (2)

user4486345
user4486345

Reputation:

You can write your .htaccess like this

    RewriteEngine On
    RewriteRule ^([^/]*)/([^/]*)$ /car.php?name=$1&car=$2 [L]

Upvotes: 4

anubhava
anubhava

Reputation: 784968

You can use this rule in your site root .htaccess:

Options -MultiViews
RewriteEngine On

RewriteRule ^cars/([\w-]+)/(\d+)/?$ car.php?car=$2&name=$1 [L,QSA,NC]

Upvotes: 1

Related Questions