Nginx Force (Redirect) WWW.Domain.COM To Domain.COM

I know how to force and redirect to under Lighttpd web server. How do I force nginx web server to redirect to

You can easily redirect www to nowww domain for SEO and other purposes using the following syntax. Edit nginx.conf file, enter:
# vi /usr/local/nginx/conf/nginx.conf
To force to, enter:

### redirect www to nowww with client code 301 ###
      if ($host = '' ) {
         rewrite  ^/(.*)$$1  permanent;

Save and close the file. Test and reload nginx webserver, enter:
# /usr/local/nginx/sbin/nginx -s reload
Fire a webbrowser and type the url:
It should be redirected to

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 8 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • .coder Jan 13, 2010 @ 9:00

    if ($host = '' ) {
    rewrite ^$uri permanent;

  • 🐧 nixCraft Jan 13, 2010 @ 10:42

    I believe $1 does the same thing as $uri, isn’t it?

  • the_guv Jan 13, 2010 @ 15:09

    out of interest, Vivek ..


    /usr/local/nginx/sbin/nginx -s reload

    as opposed to:-

    /etc/init.d/nginx restart


    here’s the deal for redirecting to, say another site:-

    server {
    listen 80;
    rewrite ^/(.*)$1 permanent;

    like those penguins. looks like London :P

    Edited by admin – code tags added.

  • 🐧 nixCraft Jan 13, 2010 @ 15:27


    reload will only load changed config option without disconnecting http clients. This is ideal for high traffic sites or reverse load balancers i.e. gracefully shutdown the old worker processes and reload changed config options. This is like sending HUP single to nginx.

    restart will restart the webserver, close & reopen log file and disconnect http clients. This is not ideal. However, I’ve not seen /etc/init.d/nginx SysV script. As most my installation are done using source code or custom build rpms.

    I suggest you take a look at the official page for more info for reload vs restart:

    Yes config can be different. My config make sure http client do not get into any loop with if conditional.


  • the_guv Jan 13, 2010 @ 19:10

    I appreciate that, Vivek .. and that link which I’d not seen.

    .. think I’d best reconsider one or two commands on some Nginx tuts I’ve written up then!

    Would like very much your opinion if you’ve the time, sometime. It is running at about 40 parts so far but then again there’s a detailed index to pin down anything of interest:-

    Set Up an Unmanaged VPS (4 Newbies) .. The V-P-S Bible

    .. I know that sounds a bit spammy. Genuinely good to build bridges with Nginx community members tho.

    Great content you have here. I’ve skulked around this site quite a bit in the last year or so. Thank you.

  • 🐧 nixCraft Jan 16, 2010 @ 7:06


    Nice work!

  • Beau Jul 20, 2012 @ 1:07

    Generic version:

    if ($host ~* www\.(.*)) {
      rewrite ^(.*)$ $scheme://$host_without_www$1 permanent;
  • bhushan Sep 17, 2013 @ 16:35

    i need to redirect to new domain and my directory structure is change totaly so no logic behind now i want to one to one mapping how can achieve this

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum