0

I want to redirect url's like: http://subdomain.domain.com/r/wtf to http://domain.com/r/wtf

How do I do this?

BenMorel
  • 4,215
  • 10
  • 53
  • 81
Steve
  • 11
  • 2

3 Answers3

4

If is evil!. And if on every request is double facepalm ...

server {
    listen 80;
    # listen 443;
    server_name *.domain.tld;
    return 301 $scheme://domain.tld$request_uri; 
}

server {
    listen 80;
    # listen 443;
    server_name domain.tld;

    # usual lines
}
cadmi
  • 6,858
  • 1
  • 16
  • 22
-4

With a rewrite rule, I have something like this in my nginx config doing exactly that:

if ($host !~ ^(www\.kaarsemaker\.net|ip\.seveas\.net)$) {
        rewrite ^(.*)$ http://www.kaarsemaker.net$1 permanent;
}
Dennis Kaarsemaker
  • 18,793
  • 2
  • 43
  • 69
-5

The simplest answer:

if ($host !~ ^domain\.tld$){
    rewrite ^/(.*) http://domain.tdl/$1 redirect;
}

Because you've asked all subdomains to one domain, so that rule would apply to any subdomain.

Edit: That meant to be inside a server definition, the one that is not the same as ^domain.tld$.

Marcel
  • 1,575
  • 8
  • 14
  • Don't bother about using 'if' directive. I'm using in production for several years without ANY problems in servers with HUGE load. The concept that for each request you'll have a comparison and that behaviour is bad is erroneous. The first response I gave to you, as mentioned in last Edit was meant to be inside a server definition as cadmi proposed, but you have to do what fits your environment. So if you have only one server definition and still wants to redirect, USE IF. And, opposed to cadmi's suggestion to return 301, it's a BAD IDEA if you're not using CACHE-CONTROL headers. – Marcel Mar 28 '13 at 20:25