Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx subdomain rewrite

Yet another nginx rewrite rule question:

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

like image 807
deb Avatar asked May 07 '10 01:05

deb


2 Answers

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

like image 192
deb Avatar answered Nov 13 '22 13:11

deb


server {
  listen 80;
  listen 443;
  server_name ~^www\.(\w+)\.domain\.com$;
  location / {
    rewrite ^ $scheme://$1.domain.com$request_uri? permanent;
  }
}
like image 30
blueyed Avatar answered Nov 13 '22 14:11

blueyed