Nginx force www to no-www domain redirect on ServerPilot Digitalocean

Discussion in 'Internet' started by Justin, Jan 5, 2015.

  1. Justin

    Justin Administrator
    Staff Member

    Dec 6, 2014
    181
    20,114
    93
    #1 Justin, Jan 5, 2015
    Last edited: Jan 5, 2015
    Here is an example how to force no-www domain on Servepilot Digitalocean VPS servers with Nginx.

    Create /etc/nginx-sp/vhosts.d/techcat.d/0www_redirect.conf
    with code that will force all www pages 301 redirect to a naked no-www domain.
    Code:
    if ($host ~* www.techcat.ca$) {
        return 301 http://techcat.ca$request_uri;
    }
    restart nginx with the command
    Code:
    sudo service nginx-sp restart
    If you would like to force all no-www pages to www.
    Code:
    if ($host ~* techcat.ca$) {
        return 301 http://www.techcat.ca$request_uri;
    }
    More info on Nginx redirects
    https://serverpilot.io/community/articles/how-to-redirect-based-on-the-requested-domain-name.html
     
  2. screenwatcher

    screenwatcher New Member

    Apr 9, 2015
    1
    0
    1
    Thank you that worked out very nicely :)

    Do you also know how to force https to all of that?
    Right now I'm using these rules in my htaccess
    Code:
    RewriteCond %{ENV:HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
    It works but it's not an elegant solution.

    Do you know how to achieve this via conf-file?
    I tried for hours and didn't get anywhere.
     
  3. husain

    husain New Member

    Apr 10, 2015
    1
    0
    1
  4. pratik.sharma1619

    pratik.sharma1619 New Member

    Jun 18, 2017
    1
    0
    1
    Worked for me!

    Big thanks
     
  5. cnisolution2

    cnisolution2 New Member

    Aug 13, 2019
    1
    0
    1
    Male
    Website Development Company
    New Delhi

Share This Page