Nginx config for www to non-www and non-www to www redirection

There are many ways to force Nginx to use either WWW version or non-WWW version of URLs for your site.

We use following codes all the time.

Redirect non-www to WWW

Single domain

server {
        return 301 $scheme://$request_uri;

All domains

server {
        server_name "~^(?!www\.).*" ;
        return 301 $scheme://www.$host$request_uri;

From WWW to non-WWW

Single domain

server {
        return 301 $scheme://$request_uri;

All domains

server {
         server_name "~^www\.(.*)$" ;
         return 301 $scheme://$1$request_uri ;

In both cases, for other-www, we create a altogether different server { } block. IMHO, this is cleanest and optimised way to handle www to non-www and non-www to www redirection.

There are some WordPress plugins available there which can handle this at PHP-level. But for performance reason, always handle things in Nginx, that can be handled in Nginx alone! 😉

30 Responses to “Nginx config for www to non-www and non-www to www redirection”

  1. Mitesh Shah

    Instead of hardcodes http we can use $scheme

    return 301$request_uri;

    return 301 $scheme://$request_uri;

  2. Hardy Sam

    Is it true that www if not redirected permanently to non-www, will hurt site ranking and Google would see it as duplicate contents?

    • Rahul Bansal

      I am not sure about Google but technically serving same page on www and non-www (root) domain should be considered duplicate.

  3. rizky

    hello thanks for the guide, im using redirect non to www
    the code “for all domain” works for me but the single domain code is not work

  4. Dr.Phunk

    Can you tell me what’s the difference here between single and all domain.
    If I need to strip www from even all the subdirectories then what shall be used ?

  5. Dr.Phunk

    Actually, I am an apache user and very new to nginx so please tell me where to save these codes under which directory and with what file name and file type?
    As there is nothing like .htaccess for nginx

  6. Sameer Joshi

    The single domain config will add a redirection only to a specific domain that you have mentioned in the configuration. Hence, the config in the post has domain which you can replace with the specific single domain.

    The All domains config will add it to all the domains for which requests are made on the server.

    These config will be added to your Nginx configuration file. It will typically be under


    Once you add the rule test your nginx configuration by

    nginx -t

    If the test is passed then reload the Nginx process by

    service nginx reload
  7. Mardix

    I don’t know if it’s a typo or something, but for me, to redirect WWW to Non-WWW I had to flip your code this way:

    server {
    server_name “~^www\.(.*)$” ;
    return 301 $scheme://$1$request_uri;

  8. Ana

    Its not working for me. I tried many options of putting server {
    server_name “~^(?!www\.).*” ;
    return 301 $scheme://www.$host$request_uri;
    but nothing works for me.
    Here is my sites available file

    • Rahul Bansal

      I think above 2 lines are losing precedence to *

      In that case you can try adding a line like below index line inside server {..} block

      if ( $http_host ~* "www\.(.*)") {
                       rewrite ^ http://$1$request_uri permanent;

      Please be careful about first line as I am writing it without tasting.

      • Ana

        Thanks Rahul but nothing works for me:(. I added your line and same, also I added this line server {
        server_name “~^(?!www\.).*” ;
        return 301 $scheme://www.$host$request_uri;
        } also in nginx.conf file so also isnt working (basically I try all these lines on different positions and same again. when without www it shows all ok when with www Server not found. Maybe problem in this that my was never reachable? Maybe I should do something to bring this www to live site and then to redirect. ?

      • Ana

        Thanks Rahul but not success. I added this line and I was trying with different lines different positions and even different files f.e. ngnix.conf (where I should exactly add this line?)
        I think that problem can be because my www never was reachable . Maybe I should do something to make it reachable and to point to my site and then to redirect?

        • Rahul Bansal

          Sorry for delayed reply. At this point, I won’t be able to debug further via comments only.

          You need to make sure:

          1. For all desired domains, DNS lookup reaches to your server’s IP.
          2. Nginx server_name block as wildcard e.g. * and/or listening by default on that IP. “listening by default” is recommended for domain-mapping.
          3. You need to use rules like I posted in previous comment “if server_name starts with www, regex to fetch domain/hostname without www and redirect to it”

          There are few more ways and tricks but its not possible for me to debug it like this. You can give it a try, or may hire somebody as a last resort.

          • Ana

            Thanks Rahul. You idea brought me to solve this issue with redirection.
            Only I don’t know if I did it right way (to not have further problems).
            What I did: I added 2 CNAME records in my dns:
            CNAME * @
            CNAME www @
            and also your line for this article and all is redirecting now properly.
            What o you think is this all correct way as I newer heard for www line before.

          • Rahul Bansal

            You don’t need CNAME record for WWW. It will be covered by wildcard record in your case.

  9. Alexander

    Putting it in to nginx.conf inside http { } but it’s not working.

  10. Alexander

    No error, it just not redirecting from www to non-www. Using nginx as reverse proxy with apache.

  11. Fahad

    I am very impress with your all article.

    Please guide how do we enable functionality for where subdomains list come from database namse is subdomain.php . i tried following which is not working

    server {
    listen 80;
    server_name *;

    **if ($http_host !~ "^(www\.)?domain\.com"){
    rewrite !^subdomain\.php$ /subdomain.php break;

    location / {
    root /usr/share/nginx/html/;
    index index.php;

    • Rahul Bansal

      Sorry, I couldn’t understand your problem properly.

      • Fahad

        When client do visit at

        First it will auto redirect at and check rahul name my db by using php code “subdomains.php”

        & get the all profile page


        i tried followings on Nginx

        if ($http_host !~ “^(www.)?”){
        rewrite !^subdomain.php$ /subdomain.php break;

        Please Help

        • Rahul Bansal

          I assume rahul from will be passed to subdomain.php as an argument.

          Something like: subdomain.php?domain=rahul

          If that is the case, you need to change regex to something like: (please note first line)

          if ($http_host !~ "^(www.)?([^\.]*)\"){
              try_files /subdomain.php?domain=$2 =404;

          I am assuming subdomain.php is present in root.

          I think you don’t need rewrite. You just need to tell nginx to use subdomain.php in a particular case (with may be some arguments)

  12. Chirag

    I would like to redirect one page on my site to homepage. Without affecting sub-pages..

    like redirecting to without getting redirect loop in

    How can that be done? I read about modifying conf file in /etc/nginx… but in my case there is no nginx directory within etc.

    • Rahul Bansal

      In server block, add rewrite ^/abc$ last;

      ^ and $ will make sure no redirect loop starts.

  13. Sin

    Hey Rahul,

    I’ve tried this, and few more things but I can’t make non-www version to work!
    Please take a look, it all seems good:

    # I added this later, first I was using your code.. #
    server {
    listen 80;
    return 301$request_uri;

    server {
    listen 80; ## listen for ipv4; this line is default and implied

    access_log /srv/www/;
    error_log /srv/www/ debug;

    root /srv/www/;
    index index.php;

    if (!-e $request_filename) {
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    rewrite ^/demo(/[^/]+)?(/wp-.*) /demo$2 last;
    rewrite ^/demo(/[^/]+)?(/.*\.php)$ /demo$2 last;

    location / {
    try_files $uri $uri/ /index.php?$args ;

    location /demo {
    try_files $uri $uri/ /demo/index.php?$args ;

    rewrite ^/download/([^/]+)$$

    location ~ \.php$ {
    try_files $uri /demo/index.php;
    include fastcgi_params;

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|r$
    access_log off; log_not_found off; expires max;

    location = /robots.txt { access_log off; log_not_found off; }
    location ~ /\. { deny all; access_log off; log_not_found off; }

    www version is working, but non www is not redirected, no matter what I put in config file.
    I have one more domain on my VPS, and I’m not sure if that’s causing the issue (shouldn’t be, but while I was experimented I was redirected to first domain, this is second).

    Any idea?

    • Rahul Bansal

      For multi-domain setup, you need to use correct server_name first.

      Something like: `server_name *` for wildcard/catch-all subdomain setup

      Or simply `listen 80 default_server` inside server block to make sure all mapped domain requests redirect to this config block.

      For future support, please use support forum –

  14. Sin

    While trying to see in Chrome I can see “resolving host” in bottom left corner, and then “Oops! Google Chrome could not find domain”. Domain has been moved from shared hosting, where there were www version has been redirected.

    I tried deactivating plugins, it’s not helping, tried to change in db everything to www, but everything is with http://www...
    Now I’m not sure should I use different port for each website, both are on 80?

    Thank you, and sorry for the long question…. :(

Comments are closed on this article.
Please use our free support forum for support and discussions.