2016-09-26 78 views
2

我有页面重定向URL,像下面有多个参数一样。用多个参数重写URL php

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

从这个网址我带的所有参数值从数据库中取数据。

因此,当我加载此页面时,我需要显示最像用户友好的网址,如下所示。在该URL应包含的product_id只值这是我用我的重定向URL

http://example.com/93477.htm

我试图改写这个网址,如下所示。但是因为我不能使用参数“categoryId”和“keyword”

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

请人帮我做成功吧..

问候,

维涅什KUMARķ

+1

如果原始url的查询字符串中的每个参数都是u sed帮助建立一个sql查询,然后没有他们,你不会找到相同的记录。原始网址中的每个必需参数都应映射到重写网址 – RamRaider

回答

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

重写的URL:

http://example.com/93477.htm

更新1:

[QSA]已经在ab加入奥雅纳代码现在,这样的查询字符串追加为了使用PHP $ _GET

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

更新2以检索 动态参数:

编辑的代码的.htaccess使关键字和类别标识动态

+0

谢谢您的回答,但原始网址CategoryId和关键字会针对每个网址动态更改。所以在这种情况下我们应该使用你的代码。 –

+1

使用[QSA]获取附加的查询字符串。让我更新答案hold on mate –

+1

@VigneshKumar更新了代码,检查并更新我是否解决你的问题队友? –