jeudi 30 juin 2016

Nginx redirect URL into a PHP GET url

I have an nginx/PHP site.

Right now I have working URLs like:

http://example.com/search?param1=var1&param2=var2&param3=var3

Where var1, var2, and var3 are the first name, last name, and location respectively.

Question

I would like http://example.com/var1/var2/var3 to forward to http://example.com/search?param1=var1&param2=var2&param3=var3 without the url in the browser changing. So, the PHP functionality can remain but the URL looks clean and typing in a clean URL transforms it to a proper GET request.

So Far

I have my nginx file: /nginx/sites-available/default

And I have found code that people have used for forwarding from one URL to another while masking it:

location / {
    proxy_pass http://192.168.1.24;
    proxy_redirect http://www.example.com/some/path http://192.168.1.24;
    proxy_set_header Host $host;
}

But it does not seem to be able to handle the regex change that I want. Is it possible to use nginx to make these changes?

EDIT 1

This does not work, but I think it is closer:

location ~ /(?<var1>[^/])/(?<var2>[^/])/(?<var3>[^/]) {
    proxy_pass http://example.com/search.php?params1=$var1&params2=$var2&params3=$var3
    proxy_set_header Host $host;
}

Aucun commentaire:

Enregistrer un commentaire