Want more timely Centmin Mod News Updates?
Become a Member

Today i wanted to redirect all from everywhere to www. .But i got unwanted redirects

Discussion in 'Domains, DNS, Email & SSL Certificates' started by pheonis, Feb 13, 2019 at 8:57 PM.

  1. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    Today while making all non www to redirects to www version . I made this change.
    Code:
    server {
       server_name zepper.in www.zepper.in;
        return 301 https://zepper.in$request_uri;
     }
    
    
    to
    Code:
    server {
       server_name zepper.in www.zepper.in;
        return 301 https://www.zepper.in$request_uri;
     }
    
    [code/]
    
    Now first i got this "your connection is not private and its redirecting me to another website in the same server." this is so weird. im out of idea, why is this happening? how do i resolve this.

     
  2. eva2000

    eva2000 Administrator Staff Member

    38,023
    8,350
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,863
    Local Time:
    1:03 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    see instructions at centminmod.com/nginx_domain_dns_setup.html#httpsredirect is the correct way to set it up - pay attention to different way if you want redirect target being www version instead of non-www and vice versa and that the target version www or non-www is the only version listed in server_name for the 2nd/main server {} context.

    key to testing is using 302 temp redirect first in a private incognito browser session otherwise the problems you can experience may end up being due to browser caching or 301 permanent redirects unless you clear browser cache and reboot local computer(s) and even then some web browsers don't let go of 301 permanent redirect browser cache that willingly :)

    You can test in SSH via curl to check headers for location field (where the redirect goes) using the following commands:
    Code (Text):
    curl -I http://domain.com
    

    Code (Text):
    curl -I http://www.domain.com
    
     
    • Like Like x 1
  3. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    i resolved it,nevermind.
    The main issue remain unsolved that is how to redirect from https://non www to https://www always.
    I tried this
    Code:
    server {
       server_name zepper.in www.zepper.in;
       return 301 https://www.zepper.in$request_uri;
     }
    Then in dns section of namesilo i added
    Code:
    zepper.in >> my ip
    www.zepper.in>>my ip
    but im not getting the www version ,im always getting the non www version
     
  4. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    Code:
    curl -I http://www.zepper.in
    got
    Code:
    curl: (6) Could not resolve host: www.zepper.in; Unknown error
    and
    Code:
    curl -I http://zepper.in
    got this
    Code:
    HTTP/1.1 301 Moved Permanently
    Date: Wed, 13 Feb 2019 11:15:34 GMT
    Content-Type: text/html
    Content-Length: 162
    Connection: keep-alive
    Location: https://www.zepper.in/
    Server: nginx centminmod
    X-Powered-By: centminmod
    
     
  5. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    changed to temporary redirect now got response in both codes
    Code:
    -bash-4.2# curl -I http://www.zepper.in
    HTTP/1.1 302 Moved Temporarily
    Date: Wed, 13 Feb 2019 11:21:43 GMT
    Content-Type: text/html
    Content-Length: 138
    Connection: keep-alive
    Location: https://www.zepper.in/
    Server: nginx centminmod
    X-Powered-By: centminmod
    
    -bash-4.2# curl -I http://zepper.in
    HTTP/1.1 302 Moved Temporarily
    Date: Wed, 13 Feb 2019 11:21:56 GMT
    Content-Type: text/html
    Content-Length: 138
    Connection: keep-alive
    Location: https://www.zepper.in/
    Server: nginx centminmod
    X-Powered-By: centminmod
    
    But in browser it's still https://non www version is showing. I have tried different computers and cellphones. Everywhere non www version is showing in browser
     
  6. eva2000

    eva2000 Administrator Staff Member

    38,023
    8,350
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,863
    Local Time:
    1:03 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    see Nginx Vhost & NSD DNS Setup - CentminMod.com LEMP Nginx web stack for CentOS for https redirect to https you need a 3rd server{} context ;)
     
  7. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    used step by step like the below settings
    Code:
    # Centmin Mod Getting Started Guide
    # must read http://centminmod.com/getstarted.html
    # For HTTP/2 SSL Setup
    # read http://centminmod.com/nginx_configure_https_ssl_spdy.html
    
    # redirect from www to non-www  forced SSL
    # uncomment, save file and restart Nginx to enable
    # if unsure use return 302 before using return 301
     server {
       server_name newdomain.com www.newdomain.com;
       return 302 https://www.newdomain.com$request_uri;
     }
    
    server {
      listen 443 ssl http2;
      server_name newdomain.com;
      return 302 https://www.newdomain.com$request_uri;
    
      ssl_dhparam /usr/local/nginx/conf/ssl/newdomain.com/dhparam.pem;
      ssl_certificate      /usr/local/nginx/conf/ssl/newdomain.com/newdomain.com.crt;
      ssl_certificate_key  /usr/local/nginx/conf/ssl/newdomain.com/newdomain.com.key;
      include /usr/local/nginx/conf/ssl_include.conf;
    }
    
    server {
      listen 443 ssl http2;
      server_name www.newdomain.com;
    
      ssl_dhparam /usr/local/nginx/conf/ssl/newdomain.com/dhparam.pem;
      ssl_certificate      /usr/local/nginx/conf/ssl/newdomain.com/newdomain.com.crt;
      ssl_certificate_key  /usr/local/nginx/conf/ssl/newdomain.com/newdomain.com.key;
      include /usr/local/nginx/conf/ssl_include.conf;
    
    < snipped the rest of the nginx settings >
    
    Now getting
    Code:
    This page isn't working, ERR_TOO_MANY_REDIRECTS
     
  8. eva2000

    eva2000 Administrator Staff Member

    38,023
    8,350
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,863
    Local Time:
    1:03 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    that is correct but as you previously used 301 permanent redirects you could be caught in browser cache of previous incorrect redirects unlike if you used 302 temp redirects for testing

    You can test and confirm this in SSH via curl to check headers for location field (where the redirect goes) using the following commands as they won't be subject to browser caches:
    Code (Text):
    curl -I http://domain.com
    

    Code (Text):
    curl -I http://www.domain.com
    

    Code (Text):
    curl -I https://domain.com
    

    Code (Text):
    curl -I https://www.domain.com
    
     
    • Like Like x 1
  9. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    The first 3 curls pointed to
    Code:
     https://www.domain.com/
    Code:
    curl -I http://domain.com
    curl -I http://www.domain.com
    curl -I https://domain.com
    But the last one
    Code:
     curl -I https://www.domain.com
    showed this
    Code:
    Date: Wed, 13 Feb 2019 11:46:13 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    Location: https://domain.com/
    Server: nginx centminmod
    X-Powered-By: centminmod
    X-Cache: MISS
    X-Cache-2: BYPASS
    X-Xss-Protection: 1; mode=block
    X-Content-Type-Options: nosniff
    
     
  10. eva2000

    eva2000 Administrator Staff Member

    38,023
    8,350
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,863
    Local Time:
    1:03 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    that is correct then - first 3 should return https www version of domain, last is your main site https www version so should return a 200 http status and that info telling you you're access the correct site.

    All you need is now to clear your browser cache and restart your computer to empty out 301 permanent cache entries from previous attemps
     
    • Like Like x 1
  11. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    Hi Eva,
    Cleared cookies and cache, restarted the pc. but still getting too many redirect error. Tried on different mobile phones and pcs even used geopeeker to test . All showed errors. am i doing something wrong?
     
  12. eva2000

    eva2000 Administrator Staff Member

    38,023
    8,350
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,863
    Local Time:
    1:03 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    If you're behind cloudflare then cloudflare is doing the redirecting probably. You need to ensure cloudflare isn't set to force https redirects
     
  13. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    Not using Cloudflare now. The internal pages are opening except the homepage.
    The homepage is showing too many redirects error.
     
    Last edited: Feb 14, 2019 at 11:53 AM
  14. eva2000

    eva2000 Administrator Staff Member

    38,023
    8,350
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,863
    Local Time:
    1:03 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    you using any wordpress caching ? try clearing wordpress cache
     
  15. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    i am not using any cache plugin in wordpress. though i don't know if redis cache is working or not?i have installed it using your guide. while installing this website.
    I can check all internal pages are opening except the homepage. im getting this too many redirects on homepage
     
  16. eva2000

    eva2000 Administrator Staff Member

    38,023
    8,350
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,863
    Local Time:
    1:03 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    try disabling redis cache and see - could be incorrect manual implementation if you didn't do it initially with centmin.sh menu option 22 wordpress installation selected choice for redis nginx level cache
     
  17. eva2000

    eva2000 Administrator Staff Member

    38,023
    8,350
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,863
    Local Time:
    1:03 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    also ensure cloudflare page rules aren't redirecting to non-www version of domain as it seems that is what the redirect is doing www version redirected to non-www via 302 temp redirect
     
  18. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    I installed redis during wordpress installation only using 22 option. How can i flush the redis and disable it?
     
  19. eva2000

    eva2000 Administrator Staff Member

    38,023
    8,350
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +12,863
    Local Time:
    1:03 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    Flush redis and restart nginx and php-fpm
    Code (Text):
    redis-cli flushall
    nprestart
    

    I'd clear cloudflare cache too
     
    • Like Like x 1
  20. pheonis

    pheonis Member

    35
    3
    8
    Dec 21, 2018
    Ratings:
    +3
    Local Time:
    8:33 PM
    i am not using cloudflare, I have changed the nameservers in my domain manager.
     
..