使用多个参数重写URL

时间:2016-09-26 06:39:01

标签: php url mod-rewrite nginx url-rewriting

我有一个页面重定向网址,如下所示,包含多个参数。

http://example.com/search.php?layout=details&Keyword=mobile&view=product&CategoryId=1026&product_id=93477

从该URL我将获取所有参数值以从数据库中获取数据。

因此,当我加载此页面时,我需要显示最友好的URL,如下所示。在该URL中应该只包含product_id的值,我正在使用我的重定向URL

http://example.com/93477.htm

我试图像下面那样重写此网址。但是因为我无法使用参数“categoryId”和“keyword”

重写^ /([a-zA-Z0-9 / - ] +)。htm /search.php?layout=details&view=product&Keyword=$3&CategoryId=$2&product_id=$1 last;

请有人帮我成功完成..

此致

VIGNESH KUMAR K

1 个答案:

答案 0 :(得分:1)

试试这个伴侣,

RewriteEngine on
RewriteCond %{THE_REQUEST} \s/search\.php\?layout=details&Keyword=([^&]*)&view=product&CategoryId=([^&]*)&product_id=([^&]*) [NC]
RewriteRule ^ /%2/%3/%1? [L,NE,R=302]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /search.php?layout=details&Keyword=$2&view=product&CategoryId=$3&product_id=$1 [L,QSA]

原始网址:

  

http://example.com/search.php?layout=details&Keyword=mobile&view=product&CategoryId=1026&product_id=93477

重写的网址:

  

http://example.com/93477.htm

更新1:

  现在已经在上面的代码中添加了

[QSA],以便附加查询字符串以便进行检索   使用PHP $ _GET

的动态参数

http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa

更新2:

  

编辑.htaccess代码以生成关键字和CategoryId动态