2

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

1
  • 1
    If each of the parameters in the querystring of the original url are used to help build an sql query then without them you will not find the same records. Each required parameter in the original url should be mapped in the rewrittern url Commented Sep 26, 2016 at 6:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer, But in that the original URL the CategoryId and Keyword will change dynamically for every URL. So in that case shall we use your code for it.
use [QSA] to get the query string attached . let me update the answer hold on mate
@VigneshKumar updated the code , check and update me whether it solves your problem mate ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.