Question

nginex rewrite url rule URL from http://domain.com/?var_1=val_1&var_2=val2 into http://domain.com/val_1/val_2/

Posted June 19, 2015 1.9k views
NginxPHP

Hi, i want to change URL from http://domain.com/?var_1=val_1&var_2=val2 into http://domain.com/val_1/val_2/ ( http://domain.com/mobile/1-first-model/ )

where to write this rule?
thnx.

edited by kamaln7

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi! First, you’ll want to find a RegEx pattern that matches /val_1/val_2 (with an optional trailing backslash): ^/(.*?)/(.*?)/?$, then you’ll want to map val1 and val2 to a query string:

rewrite ^/(.*?)/(.*?)/?$ /?var_1=$1&var_2=$2&$args

&$args appends the rest of the original query string to the request, so that for example http://domain.com/val_1/val_2/?something=abcd works as expected.

Make sure you restart nginx after editing the config file.

Submit an Answer