Home  >  Q&A  >  body text

nginx redirects when accessing a specific page

There are multiple domain names on an nginx server. If you want to access one of the specific pages, just rewrite to google.com
The configuration is as follows, but it is found that it does not take effect. I feel that $http_host$request_uri does not match that page. .

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;
   }

}

Who knows how to write it?

迷茫迷茫2714 days ago488

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 17:12:55

    Write the host that needs to be redirected separately
    I just tried it, using $http_host$request_uri ~ (.), the matched is an empty string, I don’t know why
    If you write it alone, it will be normal
    $ http_host ~ (.) matches the correct host
    $request_uri ~ (.) matches the correct uri

    server {
        listen       80;
        server_name  www.domain2.com ;
        if (request_uri ~* hello.html) {
            rewrite ^  google.com  permanent;
           }
    }
    server {
    listen       80;
        server_name  www.domain1.com www.domain3.com ;
    }

    reply
    0
  • Cancelreply