• Support
  • Redirecting http and https to www for an HTML site

Hello,

I'm having a hard time trying to serve my website from https://www.example.com. For WordPress sites, WordPress automatically takes care of this, but this is a static site (built with Eleventy+Bulma).

What I desire:

httpS://www.example.com => 200
http://www.example.com => 301 => httpS://www.example.com
httpS://example.com => 301 => httpS://www.example.com
http://example.com => 301 => httpS://www.example.com

Default behaviour:

https://www.example.com => 200
http://www.example.com => 301 => https://www.example.com
https://example.com => 200
http://example.com => 301 => https://example.com

I've already read this post and I created a file named force-www.conf in /etc/nginx/conf.d/ with the suggested code:

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

After reloading the Nginx confix, this doesn't seem to have any effect. Even rebooting the entire server made no difference. It appears there's a config elsewhere which overrides this.

I can add the www prefix in force-ssl-example.com.conf to get this redirect working:

http://example.com => 301 => https://www.example.com

server {
        listen 80;
        listen [::]:80;
        server_name example.com www.example.com;
        return 301 https://www.$host$request_uri;
}

That leaves https://example.com => https://www.example.com which I can't figure out how to get working.

Any guidance will be highly appreciated.

Thank you!

Try adding this to your site level config:-

server {
	server_name example.com;
	include /var/www/example.com/conf/nginx/*.conf;
	return 301 https://www.example.com$request_uri;
}

Note that the include statement will include that sites ssl.conf so this block is listening on port 443 and therefore applies to https://

    Thanks for the suggestion.

    If by "site level config" you mean the config from wo site edit example.com, I just did that but saw no change. I even rebuilt the VPS so I could start from a fresh page with your suggestion, but still no change. I'm still unable to get https://example.com to redirect to https://www.example.com (without creating a redirect loop).

    Hmm - you are keeping the server blocks separate (not nested)?
    I'm currently using the block I posted on a few different sites.
    Maybe post your full site config (yup, wo site edit example.com)?

    I just rebuilt the VPS again and got it working. Thanks a million for helping me troubleshoot this!

    I was actually keeping them in separate server blocks, but I was making two rookie mistakes:

    First, I had not removed example.com from the server_name line in the first block, so the second server block for example.com was completely ignored. Funny thing is, I actually thought of this, but I guess I missed the memo that said Nginx is not CSS where the last declaration wins 😀

    And when I realized my folly and fixed this, I also had return 301 https://www.$host$request_uri; in /etc/nginx/conf.d/force-ssl-example.conf --- and this was creating a redirect loop for www.example.com. I had to change the $host variable to the actual domain (ie return 301 https://www.example.com$request_uri; ) to stop this redirect loop.

    So now all roads lead to www.example.com, and I'm good to go.

    Thank you again!

      4 days later

      georgeappiah hey bro will help me how to do this ? Actually i want a single wordpress site redirect to www will explain all step's it will be Very helpful for me

        JamilKhan

        With a WordPress site, you don't have to do any Nginx config changes. Just make sure both your WordPress Address and Site Address (configured at SETTINGS => GENERAL in your WordPress dashboard) y begin with www, and WordPress will take care of the redirection.

        4 years later
        a month later

        Hosted by VirtuBox