• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

Nginx config: Redirect www requests to non-www

Scheduled Pinned Locked Moved Solved Technical Support
nginx
6 Posts 3 Posters 3.4k Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • henrywrightH Offline
    henrywrightH Offline
    Henry Wright
    wrote on last edited by
    #1

    I switched over to Nginx from Apache and am using the SSL config here. This works great but I'd like to modify it so that all requests to www are redirected to non-www.

    Can anyone help?

    1 Reply Last reply
    0
  • KowlinK Offline
    KowlinK Offline
    Robert Translator
    wrote on last edited by
    #2

    I'm not sure if you're using Cloudflare. If you do you can make a Pagerule that redirects any www. to the main URL. http://i.imgur.com/E10DLXE.png

    If you just use Nginx, this should help you on your way. http://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www

    1 Reply Last reply
    0
  • zerefelZ Offline
    zerefelZ Offline
    zerefel
    wrote on last edited by zerefel
    #3

    Sure thing. Navigate to your server config file (/etc/nginx/sites-availble/your_file) or wherever you keep it and add one more server block before your main server block.

    Important: Don't use rewrite, as that's a common pitfall, as stated here

    So with that said, to canonicalize your domain:

    -www to no www:

    server {
        server_name www.example.com
        return 301 $scheme://example.com$request_uri;
    }
    

    To canonicalize your IP:

    server {
        server_name your:ip:address:here;
        return 301 $scheme://example.com$request_uri;
    }
    

    And then finally your main server block:

    server {
        listen 80;
        server_name example.com;
        [...]
    }
    
    1 Reply Last reply
    0
  • henrywrightH Offline
    henrywrightH Offline
    Henry Wright
    wrote on last edited by henrywright
    #4

    Thanks for your help, these responses have been very useful. Also, if anyone is using Digital Ocean and stumbles upon this topic, I found a pretty good tutorial here.

    KowlinK 1 Reply Last reply
    1
  • KowlinK Offline
    KowlinK Offline
    Robert Translator
    replied to Henry Wright on last edited by
    #5

    @henrywright I kinda avoided quoting Digital Ocean as I found there guide a bit confusing.

    henrywrightH 1 Reply Last reply
    0
  • henrywrightH Offline
    henrywrightH Offline
    Henry Wright
    replied to Robert on last edited by
    #6

    @Kowlin some of their tutorials are confusing but some I've found to be excellent. I think it depends on the tutor perhaps?

    1 Reply Last reply
    0

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development