2

I have a site running on nginx.

in my nginx/sites-available/default file I have a line:

rewrite ^(.*)$ /search.php?path=$1 break;

What this does is make examplesite.com forward to examplesite.com/search.php?path=$1 and then mask that new URL. So, the url looks like "examplesite.com" and operates like examplesite.com/search.php?path=$1

I would like to do something different.

I would like to not at all change the functionality of examplesite.com, but instead make the url examplesite.com/search.php?path=$1 to appear as examplesite.com/$1 and keep its same functionality.

So I do not want to redirect, just make examplesite.com/search.php?path=$1 appear as examplesite.com/$1

But, it seems that rewrite cannot handle this functionality. Is it possible to have that functionality in nginx?

EDIT 1

For example:

I have the url http://www.examplesite.com/search.php?search=house&location=chicago

I would like to keep the functionality, but just make it appear as:

http://www.examplesite.com/chicago/house

EDIT 2

I have code that changes:

http://www.examplesite.com/search.php?search_title=apple&search_location=boston

to:

http://www.examplesite.com/boston/apple?search_title=apple&search_location=boston

This is the code:

if ($args ~* "search_title=[a-z]*&search_location=[a-z]*") {
    rewrite ^/search.php?(.*)$ http://www.examplesite.com/$arg_search_location/$arg_search_title last;
}

But the issue is that this makes the PHP code on the site fail, and it still displays all the parameters at the end.

EDIT 3

After looking at this: https://serverfault.com/questions/160790/nginx-rewrite-for-an-url-with-parameters as suggested in the comments, I can now display the proper the url:

http://www.examplesite.com/boston/apple

By adding a ? at the end making my current code this:

if ($args ~* "search_title=[a-z]*&search_location=[a-z]*") {
    rewrite ^/search.php?(.*)$ http://www.examplesite.com/$arg_search_location/$arg_search_title? last;
}

But this url is the url that the PHP is reading. So, I get a 404 Not Found which makes sense.

Is it possible to make the PHP read the old URL but have the url in the browser as it is now?

EDIT 4

I am unsure, but I am thinking that nginx's rewrite function may be incapable of doing what I want, so I am looking at proxy_pass and proxy_set_header

EDIT 5

I have a 2nd question running that approaches the problem from a separate angle here: Nginx redirect URL into a PHP GET url

Community
  • 1
  • 1
Rorschach
  • 3,104
  • 3
  • 24
  • 60
  • 1
    First try `nginx -t -c /path/to/your/nginx.conf` to see that the config is fine, second, make sure you're using capturing groups since I'm not sure you'll be able to change and use the parameters location this way, BTW, I found this SO Answer: http://serverfault.com/questions/160790/nginx-rewrite-for-an-url-with-parameters – Yaron Jun 26 '16 at 07:08

0 Answers0