nginx map request and return with no effect

ceph3us asked:

i want to rewrite/return request to new one

i have

  • map

    map $request $new_request {
        include url_rmap.conf;
    }
    
  • file url_rmap.conf

    #here i have tried  all variations
    $scheme://d.dom https://any.other/address
    $scheme://d.dom/ https://any.other/address
    https://d.dom https://any.other/address
    https://d.dom/ https://any.other/address
    
  • server block

    server {
    
         listen ip:port;
         server_name a.dom b.dom aa.a.dom w.c.dom d.dom;
    
         # first try 
         if($new_request) {
               return 301 $new_request;
         }
    
         # also second with @redirect-nap
         location / {
            # Support Clean (aka Search Engine Friendly) URLs
            try_files  @redirect-map $uri $uri/ /index.php?$args; 
         }
    
         location @redirect-map {
              if ($new_request) {  # redirect if the variable is defined
                    return 301 $new_request;
              }
         }
    
    }
    

both ways with call to https://d.dom fails to redirect / no effect why ?

Why are you trying to map on $request? – Michael Hampton♦

i can count so many…. reasons

1) because i can ??? almost can ?
2) i want to change external ugly url like:

   https://shitgoogle.com/saome.stupidaddres/c/ssss/11232354256goofle 

to my subdomain:

  https://gc.dom.com   :) 

and the second one:

2) is short
3) is nice
4) is whatever i want
5) independent
5) is mine !!!
6) etc…

besides that name and services are bound

My answer:


You’re using the wrong variable.

You’re trying to map on the $request variable, which does not do what you say you want.

This variable, as documented, contains the entire request line from the HTTP request. For instance:

GET / HTTP/1.1

To match the URL the user used, you would first have to reconstruct it. No single variable contains this information. For example:

map $scheme://$host$request_uri $new_request {

And that still might not be the best way of doing … whatever you’re doing.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.