Nginx rewrite non-www-prefixed domain to www-prefixed domain

后端 未结 7 1120
旧时难觅i
旧时难觅i 2021-01-30 11:05

I see the Nginx HttpRewriteModule documentation has an example to rewrite a www-prefixed domain to a non-www-prefixed domain:

if ($host ~* www\\.(.*)) {
  set $h         


        
7条回答
  •  抹茶落季
    2021-01-30 11:36

    if ($host ~* ^[^.]+\.[^.]+$) {
        rewrite ^(.*)$ http://www.$host$1 permanent;
    }
    

    It's only possible to get valid hostnames because the request will never make it to your server otherwise, so there's no need to build your own validation logic.

提交回复
热议问题