Monday, May 30, 2016

redirect - NGINX URL with parameter rewrite to URL without parameter




I have an NGINX background on my server. I cannot figure out why my rules doesn't work.



How can I redirect or rewrite it so I could open URL with the same content, for example:



http://www.example.com/my_dir/value/


If I have this URL, for example:



http://www.example.com/my_dir/filename.php?parameter=value



I tried some solutions but they didn't worked:



Case #1:
location /my_dir/ {
if ($args ~* "filename.php/?parameter=value1") {
rewrite ^ http://www.example.com/my_dir/$arg_param1? last;
}
}


Case #2:
rewrite ^/my_dir/(.*)$ /filename.php?parameter=$1 last;

Case #3:
location /my_dir {
try_files $uri $uri/ my_dir/filename.php?$args;
}

Case #4:

location /my_dir/ {
rewrite ^/my_dir/filename.php?parameter=(-+([a-zA-Z0-9_-]+))?\$ /filename.php?parameter=$1 last;
}


Is that possible? Do I have to configure my PHP file for get it working?



Any ideas?



Thanks!



Answer



Visit : domain.com/folder/abc
--> domain.com/folder/filenam.php?url=abc



//this is inside the http block
map $request_uri $request_basename {
~/(?[^/?]*)(?:\?|$)
$captured_request_basename;
}


server {
[snip]
location /folder {
try_files folder folder/ /folder/filename.php?url=$request_basename;
}
}

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...