如何重写使用.htaccess的get变量的url

时间:2016-02-21 09:49:09

标签: php apache .htaccess mod-rewrite

我正在尝试编写一个能让我写

的.htaccess文件
  

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

而不是制作网址:

  

sampleurl.com/cars/Audi-A4/1

我目前的代码是:

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

有什么想法我怎么能让这项工作?

修改

两个代码都有效:

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

Options -MultiViews
RewriteEngine On

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

但是当我打开网站时,它只显示页面,没有链接的css文件,如何将css文件链接到该网页?

当前的CSS链接代码:

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

2 个答案:

答案 0 :(得分:4)

您可以像这样编写.htaccess

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

答案 1 :(得分:1)

您可以在站点根目录中使用此规则.htaccess:

Options -MultiViews
RewriteEngine On

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