nginx.conf redirecta a full url

1k views Asked by At

I want to redirect a full url using nginx. This doesn't work:

server {
    listen       80;
    server_name  www.domain1.com www.domain2.com www.domain3.com ;

if ($http_host$request_uri ~ www.domain2.com/hello.html) {
    rewrite ^  google.com  permanent;
   }

}

What is the correct way to do this?
I doubt whether the variable $http_host$request_uri can match to that address.

1

There are 1 answers

0
Nav On

Apparently if cannot deal with expressions very well. I'd rewrite your configuration like this:

server {
    listen       80;
    server_name  www.domain1.com www.domain2.com www.domain3.com;

    set $full_url $http_host$request_uri;
    if ($full_url ~ ^www\.domain2\.com/hello\.html) {
       return 301 https://google.com;
    }
}

It might be too late for you but It resolved my problem and I hope it helps someone...