使用多个参数清除URL

时间:2018-06-20 07:56:56

标签: regex mod-rewrite clean-urls

我正在使用.htaccess文件实现干净的url,但是问题是,我无法获取第二个参数。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9]+)$ result-by-city.php?city=$1

RewriteRule ^\/(searedness)([0-9]+)(.*)-gu([0-9]+)\/(.*)$ ?searedness$2=$4&%{QUERY_STRING}[L]

</IfModule>

如果我添加第二个参数

RewriteRule ^([a-zA-Z0-9]+)$ result-by-city.php?city=$1&location=$2

然后,页面转到404

http://example.com/delhi/noida->返回404

http://example.com/delhi/->仅适用以下规则即可

RewriteRule ^([a-zA-Z0-9]+)$ result-by-city.php?city=$1

1 个答案:

答案 0 :(得分:1)

您可以将此规则用于:city/location/speciality

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+)(?:/([^/]+))?)?/?$ page.php?city=$1&location=$2&speciality=$3 [L,QSA]

这条规则将支持所有这些URIL:

example.com/delhi
example.com/delhi/noida 
example.com/delhi/noida/dentist