Bert
Bert

Reputation: 377

NginX redirect if URL equals

My issue is with NginX anf if (I know if is evil, but I need to use it). I want to redirect the users coming to my website uon two conditions. If their IP matches and if the URL is exactly www.domain.com

Base is I have a webserver with 2 IPs and 2 domains. One for English, one for local language. I want to redirect everyone with a local IP to the local domain. (from domain.com to domain.hu)

What I've done so far: I've installed NginX with IP2Location and it works like a charm. Here is my "if" part of the nginx.conf

if ($ip2location_country_short ~ ^HU$)
            {
            set $my_var1 "${my_var1}B";
            }
    if ($request_uri ~ .*wonder*.*)
            {
            set $my_var1 "${my_var1}C";
            }
    if ($my_var1 = ABC)
            {
            #rewrite ^/(.*)$ http://www.domain.hu last;
            }

What I want to achieve is: Use a variable called $my_var1 and use it to decide if I need to redirect the user or not. I've set it to "A" in the 1st place. In the first condition: If the user is from Hungary then make $my_var1 equal to "AB". Next I want to check if the user have written the ENG or the HU URL in the browser. I want to redirect the user if he wrote an exact URL of www.domain.com. If he did, then I set my $my_var1 to "ABC". And in the 3rd if, if the value of $my_var1 equals "ABC", then inicialise the redirection.

My problems are: I don't know how to check the URL. I found this problem while I wrote a PHP file to show me the value of $my_var1 after entering the site and it turned out the value of $my_var1 os always only "AB". I'm failing to check the URL. Can you please give me a description how to check the URL conataining text/equal to something

Upvotes: 0

Views: 9237

Answers (1)

Bert
Bert

Reputation: 377

Done!

set $my_var A;

    if ($ip2location_country_short ~ ^HU$)
            {
            set $my_var "${my_var}B";
            }
    if ($http_host ~ .*domain.com.*)
            {
            set $my_var "${my_var}C";
            }
    if ($my_var = "ABC")
            {
            rewrite ^/(.*)$ http://domain.hu last;
            }

Didn't think it will be this easy.....

Upvotes: 2

Related Questions