Redirect all to www

Hello friends!

I am a beginner with use of servers and Easy Engine.

And I need a little help from you.

I have a server in Digital Ocean with ubuntu 16.04.2 64x I installed my wordpress using the Easy Engine this everything working perfectly, but unfortunately I could not redirect all the domains to open with WWW.

I have seen some posts here talking about it, but since they are old I was afraid to follow the procedure because I did my installation with the latest version of Esy Engine and may have changed something.

Could someone help me with this please.

Here is the template of my name server:

server {

server_name meusite.com.br   www.meusite.com.br;


access_log /var/log/nginx/meusite.com.br.access.log rt_cache;
error_log /var/log/nginx/meusite.com.br.error.log;


root /var/www/meusite.com.br/htdocs;



index index.php index.html index.htm;


include common/php.conf;
include common/wpcommon.conf;
include common/locations.conf;
include /var/www/meusite.com.br/conf/nginx/*.conf;

}

}

Thanks in advance for any support.

Hm my settings are similar to yours, and I’m running on www. Did you set your site address in wp as http://www. (or https if you’re using it)

Thanks for the feedback, @msarhan

Yes it works with www, but if someone type without www it will open like this (mysite.com) but I want it to always open with (www. mysite.com.br)

And in my wordpress already everything is configured with www as the image shows.

Edit: If you saw my post earlier about editing nginx.conf please delete that line modification and follow the steps below.

Ok, we want to tell the HTTP server nginx is running to do a 301 (moved permanently) redirect to HTTPS.

sudo nano /etc/nginx/conf.d/force-ssl-meusite.com.br.conf

Paste in the following

server { listen 80; server_name www.meusite.com.br meusite.com.br; return 301 https://www.meusite.com.br$request_uri; }

Press Ctrl+X. Press Y to confirm you want to save the document and then hit enter.

Next, restart nginx

sudo systemctl restart nginx

You could even take out the www. part in the 301 redirect line, making it return 301 https://meusite.com.br$request_uri; - Wordpress will handle the redirection to www at that point, no matter what the person has typed in. I used to do my 301 redirect as https://www. but now I do it without the www (just https), and wordpress does the rest for me. So if someone goes to mydomain.com or www.mydomain.com they’re always redirected to https://www.mydomain.com

My TTFB (time to first byte) has gone down from 240ms to 60ms this way. I don’t know why, but if I did the 301 redirect to https://www instead of just https:// my TTFB would be 240ms.

This will cause a redirection loop. Try and see by yourself.

@portofacil That’s how mine is set up, works perfectly.

@fbsena is that your actual domain? I just checked and it doesn’t have SSL configured (and it’s not running EE) so I’m guessing it’s not. Replace that domain with yours in the config and in the file-name when you’re making that file.

Hello friends @msarhan @portofacil, now I’m confused what should I do?

My site has ssl configured with let’s encrypt the site is that https:// www. site .com.br

I was avoiding putting here so as not to create link.

Thanks in advance for any advice!

@fbsena

That’s ok feel free to edit your post and remove the link. Sorry I shouldn’t have asked I just wanted to make sure it was your domain so I could tell you to edit the config to change it to your own domain.

So to recap,

Open SSH. Type in sudo nano /etc/nginx/conf.d/force-ssl-yourdomainname.com.br

Paste the following

server {
listen 80;
server_name www.yourdomainname.com.br yourdomainname.com.br;
return 301 https://yourdomainname.com.br$request_uri;
}

You could put www after the https but since you’ve told wordpress to do the www, this way is actually faster. It will work either way though, with or without the www, I just tested it myself.

Press ctrl+x to save, y to confirm and enter. Then reload nginx by typing sudo systemctl reload nginx. If somehow you get a redirect loop just open the file again and put a # at the beginning of every line, then reload nginx again.

Don’t take my word if you don’t know who to trust. Instead, follow the link above and try.

My 301 redirect setup does not create a redirect loop. I tried it both ways as you can see, with and without the www (but with https, as WordPress handles the redirection to www.

Click through on image to see what I mean. I restarted nginx and retrieved the page headers after the changes to show you.

Imgur

Thanks for the help, but it did not work here!

@portofacil sorry friend if you made me think that I do not trust your word I honestly did not want to say it.

I just said I was confused and unfortunately the previous process that @msarhan did not work with me.

I’ve seen several explanations on google, but I was left wondering where to put the codes.

http://hamletbatista.com/seo-geek-school/redirect-www-non-www-nginx/

Would you do this procedure as?

It would be of great value if you show how you configure this redirection. Even for other people who have the same problem.

When you installed EE, did you use the --LE tag? You may have another conf file that is contradicting this one.

Can you ls -la in that directory please?

Edit: I am confused. How could this redirect method be working for me and not for you? This is the only time on my server that the return directive is ever mentioned.

And then in /var/www/mydomain.com.au/conf/nginx/ssl.conf I’ve got this:

listen 443 ssl http2;
ssl on;
ssl_certificate     /etc/letsencrypt/live/mydomain.com.au/fullchain.pem;
ssl_certificate_key     /etc/letsencrypt/live/mydomain.com.au/privkey.pem;

Yes I used this code (wget -qO ee rt.cx/ee && sudo bash ee)

My nginx.conf https://pastebin.com/u8CGqqDB My ssl.conf (file location in previous post) https://pastebin.com/bTF6LB3k My vhost file https://pastebin.com/Vu4MsfPB

This is how EE set everything up when I installed it with the --LE tag. The only (unrelated) changes I’ve made are in nginx.conf, so I don’t understand why it’s not working for you

You should edit your vhost file.

Edit the current server block in order to have only the www version of the domain; after that you manually add a new server block without www, just with the redirect rules.

Hi @portofacil

Unfortunately it does not work I’ve done it in several ways, but it gets looped. I do not know what could be happening. I just did the easy install with wordpress.

Would you have any more tips?

Thank you very much in advance!

Sorry, I don’t know what else to suggest. :frowning:

Thank you, my friend, thank you for your attention!

Hi fbsena,

I had same issue.

Just change your line server_name meusite.com.br www.meusite.com.br;

to server_name meusite.com.br;

This works for me. In WP pannel my site is configured with https://www.meusite.com.br in both fields And i have letsencrypt protocol.

Bests.

2 Likes