.htaccess重写规则忽略字符串,url中的多个GET变量

时间:2015-04-18 01:27:08

标签: apache .htaccess mod-rewrite

我正在尝试制作.htaccess重写规则来映射4个不同的get变量并排除一个字符串。字符串是不可更改的,即。永远都会保持不变。

目前的网址是:

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

应该是这样的:

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

到目前为止我已经完成了

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

现在我需要在漂亮的网址末尾忽略字符串 -for-sale

任何帮助都非常感谢!

2 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:1)

您的分隔符是连字符而不是正斜杠,因此您的RewriteRule也应相应处理:

Options -MultiViews
RewriteEngine On

RewriteRule ^car/^([^-]+)-([^-]+)-([^-]+)-([^-]+) car.php?make=$1&model=$2&year=$3&color=$4 [L,QSA,NC]
  • MultiViews使用选项Apache's content negotiation modulemod_rewrite之前运行,并使Apache服务器匹配文件扩展名。因此/file可以在网址中,但它会投放/file.php