简体   繁体   中英

Rewrite URL with multiple parameters php

I have page redirect URL like below with more than one parameters.

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

From that URL i'm taking all parameter values to take a data from database.

So when i load this page i need to show URL most user friendly like below. In that URL should contain only the value of product_id which is i'm using my redirect URL

http://example.com/93477.htm

I have tried to rewrite this URL like below. But in that i couldn't use the parameters "categoryId" and "keyword"

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

Please anyone help me to done it successfully..

Regards,

VIGNESH KUMAR K

Try this mate ,

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]

The original URL:

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

The rewritten URL:

http://example.com/93477.htm

Update 1 :

[QSA] has been added in the above code now so that to append the query string in order to retrive dynamic parameters using PHP $_GET

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

Update 2:

Edited the .htaccess code to make Keyword and CategoryId Dynamic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM