deb
deb

Reputation: 12814

nginx subdomain rewrite

Yet another nginx rewrite rule question:

How can I do a rewrite from http://www.*.domain.com to http://*.domain.com?

Upvotes: 6

Views: 8761

Answers (2)

blueyed
blueyed

Reputation: 27858

server {
  listen 80;
  listen 443;
  server_name ~^www\.(\w+)\.domain\.com$;
  location / {
    rewrite ^ $scheme://$1.domain.com$request_uri? permanent;
  }
}

Upvotes: 2

deb
deb

Reputation: 12814

if ($host ~* www\.(.*)) {
  set $host_without_www $1;
  rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains '/foo', not 'www.mydomain.com/foo'
}

Answer from server fault: https://serverfault.com/questions/139579/nginx-subdomain-rewrite

Upvotes: 7

Related Questions