Redirecting WWW to NON-WWW (Nginx only)

Redirecting WWW to NON-WWW is very important for SEO and I miss it a lot as a default option in HestiaCP.

I know it can be done implementing this, but since I already redirected HTTP to HTTPS from that check option when I added my website, now I don’t know what the exact file I need to go, and what block I should edit, should it be better to add a new block instead for better performance or edit the existed block. There are many of them in these files which made me confused! :frowning:

/etc/nginx/conf.d/myIP.conf
mysite.com.conf
mysite.com.ssl.conf
/etc/nginx/nginx.conf

Hope someone can tell me how to do it.

You need to create a new template from two files (tpl and stpl) in the directory:
/usr/local/hestia/data/templates/web/nginx/php-fpm

Add this entry in the * .stpl file in the server section:

if ($host ~* www.(.)) {
set $host_without_www $1;
rewrite ^(.
)$ https://$host_without_www$1 permanent;
}

Then open the domain editing in the HestiaCP panel and select the created template.

1 Like

Thanks, Dmitry! But that didn’t work for me, I ended up with that black page error! Not sure if this is related but I didn’t install Apache, I’m just using Nginx instead.

By the way, should nonwww.tpl I’ve created be empty and only put the snippet you gave in the stpl version?

The above code was copied incorrectly for me and cannot be edited. Try to take this code on pastebin:

I am using Nginx + php-fpm.

The tpl and stpl files should be roughly similar to each other.
I can show my template for wordpress as an example:
wordpress2.tpl - https://pastebin.com/qFL5m0Dg

wordpress2.stpl - https://pastebin.com/K4y3reYc

This template redirects from http to https and www to non-www.

I’m not using WordPress in my business so I think this is not the right solution for me.

But thanks for trying to help me out, appreciate it.

I showed an example of implementing a redirect from www to non-www in HestiaCP templates. I hope this helps you create your own template for your needs.

so I have solved it, redirected with 301

www to non-www
nginx-nonwww.stpl https://pastebin.com/CQWhq90R
nginx-nonwww.tpl https://pastebin.com/1p4CQgMt

non-www to www
nginx.stpl https://pastebin.com/A3zJWXmH
nginx.tpl https://pastebin.com/AVf3V2eR

1 Like

Actually, I solved it without creating or editing any template file. Simply I did this:

nano /home/user/conf/web/domain.com/nginx.ssl.conf

Adding this below server_name line:
if ($host != “domain.com”) { return 301 https://domain.com$request_uri; }

systemctl restart nginx

Will break on next update…

You will have to edit the template you use… and preferable by creating a new template.

Oops! Didn’t know that. I’ll try creating a template and hope tuxuser template will work for me.

Thanks, dude! It works now perfectly.

You got a fat header which informs you about it in every config :grin:.

1 Like

A post was split to a new topic: Non www, www redirect