Non-www to www redirect on Multisite created using EasyEngine

Based in configuration, I have mentioned here: 400 Bad Request after installing LetsEncrypt SSL on WordPress Multisite installation
I created a WordPress Multisite installation with subdomains and mapped domains capability using EasyEngine commands.

Have successfully installed SSL for all the subdomains and mapped domains ( but not using easy easyengine commands like ee site create example.com --wp --lets encrypt ) but using different independent approach similar to https://easyengine.io/tutorials/nginx/letsencrypt/ )

Everything is set and working but the thing is - I want to have www in my URL. It wasn’t my choice, but I have noticed that by default easyengine creates site without www URL.
Though it’s easy to get WWW URL with WP single installation ( by modifying home url and site url in wordpress dashboard - general settings) and there are various options for multisite as well, but nothing worked for me ( I have searched the whole internet, literally :slight_smile: )
One very exciting solution was to change Home URL and Site URL for all multisites sites as well using this tutorial: https://premium.wpmudev.org/forums/topic/how-to-change-url-for-wp-multi-site?fbc=b
It worked for the primary domain ( in my case www.presspeace.com ) but not for other subdomains and mapped domains.

It gave ‘INFINITE REDIRECT LOOP’ issue.

Other server-side method include things written here: https://easyengine.io/tutorials/nginx/www-non-www-redirection/
Well I tried it as well but it gives this weird error:

Yes, it somehow adds ‘TWO WWW’, don’t know why.

I have tried adding redirection code to ssl.conf and domain.com conf too, but same issue. Most relatable and my current configuration for related files are:

/etc/nginx/sites-enabled/presspeace.com and /etc/nginx/sites-available/presspeace.com both conf:

server {

# Uncomment the following line for domain mapping
listen 80;

server_name presspeace.com *.presspeace.com www.presspeace.com test.presspeace.com www.test.presspeace.com mappedomain.com www.mappedomain.com ;

# Uncomment the following line for domain mapping
server_name_in_redirect off;

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

root /var/www/presspeace.com/htdocs;

if ( $http_host ~* "(?!www\.).") {
             rewrite ^ https://www.$host$request_uri permanent;

}

index index.php index.html index.htm;


include common/wpfc.conf;      
include common/wpcommon.conf;
include common/locations.conf;
include /var/www/presspeace.com/conf/nginx/*.conf;

}

Notice that I have insterted this redirection code under above shown conf file:

if ( $http_host ~* "(?!www\.).") { rewrite ^ https://www.$host$request_uri permanent; }

I have deleted a file named ‘default’ from both
/etc/nginx/sites-enabled/ and /etc/nginx/sites-available/ as instructed at some place.

My SSL.conf file looks like this under: /var/www/presspeace.com/conf/nginx

listen 443 ssl http2; ssl_certificate /etc/letsencrypt/live/presspeace.com-0002/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/presspeace.com-0002/privkey.pem; if ($scheme = http) { return 301 https://$host$request_uri; }

I also tried adding this file to presspeace.com nginx conf:

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

`
But all these gives the ‘TWO TIMES WWW’ (ie. https://www.www.presspeace.com/ ) ERROR.

I am no developer so, whatever I do or have done is with the help of common sense and internet friends.

Kindly see if you can give any further input on this.

Thank you.

Seems like $host$request_uri is returning www.presspeace.com instead of presspeace.com that you want (since you already have included www in your rules).

The easient option would be to simply remove the static www from your rule I assume, however, in your case, I would investigate the possible reasons for the www-added returned $host$request_uri.

Hey @ggloveswp, Thanks for the reply, I just tried what you said and removed www from there, but it starts giving this error "This page isn’t working

domain.com redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS"

:frowning:

Can you paste your error.log’s latest lines here @nanowhiz ?

Hi,

error.log (22.7 KB)

( error log copied from /var/log/nginx)

Sure. Just attached here. But I don’t see any correlation between the error code and files here. Also, the time is not matching with the actual time. How can I sync the time?

Also, when I try adding code to the top of /etc/nginx/sites-enabled/presspeace.com like this:
server {
server_name “~^(?!www.).*” ;
return 301 $scheme://$host$request_uri;
}

Here, I have removed www, but this time, nothing happened. Site opened but as usual, I mean without www and without any errors.

I also noticed that when I try to open any created subdomain like test.presspeace.com it redirects to presspeace.com while when I open the mapped domain ( say md.com mapped to test.presspeace.com ), it opens md.com ( and doesn’t redirect to the presspeace.com ) Just telling you to guess the error.

presspeace.com.error.log (9.4 KB)

One more error log named presspeace.com.error.log’ from the same folder

You can reconfigure date using dpkg-reconfigure tzdata command.

Please try the following:

Step 1: Remove every custom part of the code from /etc/nginx/sites-available/presspeace.com.

Replace this part of the code:

server {

# Uncomment the following line for domain mapping
listen 80;

server_name presspeace.com *.presspeace.com www.presspeace.com test.presspeace.com www.test.presspeace.com mappedomain.com www.mappedomain.com ;

# Uncomment the following line for domain mapping
server_name_in_redirect off;

with this one:

server {
listen 80;
server_name presspeace.com www.presspeace.com;

and remove completely this part of the code:

if ( $http_host ~* "(?!www\.).") {
             rewrite ^ https://www.$host$request_uri permanent;

Do not worry for sites-enabled directory. This is just a dynamic symlink to sites-available folder.

Step 2: Remove every custom part of the code from /var/www/presspeace.com/conf/nginx/ssl.conf:

Remove this part of the code (nothing more-nothing less):

if ($scheme = http) {
return 301 https://$host$request_uri;
}

Step 3: Reload or restart your stack.

After this step, you should be able to access wp-admin with no HTTP errors. Okay - Lets fix the www issue now.

Step 4: Access wp-admin and go to Settings -> General. Make sure that you update your URLs from https://presspeace.com/ (or http, if exists) to https://www.presspeace.com/

Alternative to step 4: Use this software and replace every http:// instance to https://, then every https://presspeace.com to https://www.presspeace.com. Make sure that you choose live run.

You should be able to access your website just fine by clicking here: https://www.presspeace.com and by clicking here, it sould redirect to the www URL: https://presspeace.com

  • If non-www is not redirecting to the www automatically, you need to edit /etc/nginx/sites-available/presspeace.com again.

Edit it and replace this part of the code:

server_name presspeace.com www.presspeace.com;

with this one:

server_name www.presspeace.com;

and then, go to the end of the file (after closing } ) and paste the following code:

server {
    listen 80;
    server_name presspeace.com;
    return 301 http://www.presspeace.com$request_uri;
}

Reloading your nginx config after the last step, (either by service nginx reload, or by reloading stack) should help you solve this issue forever :slight_smile:

Hi @ggloveswp, First of all, thank you so much for taking time to understand my issue and trying to resolve it.

Worked like charm. Thank you :slight_smile:

For the bigger part of solution that you have provided above. It appears that you misunderstood two things:

  1. I am wordpress multisite with subdomains and having server_name as *.presspeace.com is necessary to be able to create new subsites with subdomains.
  2. I am looking for a solution to be able to redirect all kinds of URL ( non-https, https, non-www, www) to www.domain.com ( here domain can be presspeace.com, subdomain or mapped domain)

As mentioned here- 400 Bad Request after installing LetsEncrypt SSL on WordPress Multisite installation , I have already successully installed everything on multisite as well, but while trying to redirect all urls to its WWW version, I started getting this TWO WWW errors .

Though I tried to do what you have said above again and it successfully let me open https://www.presspeace.com/ , but other subdomains and mapped domain doesn’t work with this.

I am sorry if I was not clear enough with my post that it’s multisite setup ( though I have mentioned it in the Post title), but do give it a re-look in a multisite perspective, if possible.

Thanks and regards, Nitesh

Btw - I assume DNS settings are correct? I’ll dig in to this later on today.

DNS Settings: You need to create a “A” record with an asterix (*)in your DNS records, plus you “need” to give an A instead of CNAME for your www.

He needs a multidomain, so it should be presspeace.com *.presspeace.com

Hey @hakabe, thanks for coming in.[quote=“hakabe, post:9, topic:8799”]
I assume DNS settings are correct? I’ll dig in to this later on today.
[/quote]

Yes, I use Cloudflare for my DNS management.

My primary domain is pointed correctly to the server IP with A records for both www and non-www versions of wildcards, domain and subdomains as well.

Other mapped domains have also been pointed correctly with A-records to the www and non-www versions both. [quote=“hakabe, post:9, topic:8799”]
DNS Settings: You need to create a “A” record with an asterix (*)in your DNS records, plus you “need” to give an A instead of CNAME for your www.
[/quote]

Yes, It’s always A on my side. Have tried with CNAME for mapped domains as well, but results remain same.

I will wait for your further response on this.

Cheers,
Nitesh

Actually all the A records are worthless when using a wildcard - at least I think so (no expert on these). I’m having a hard time following the configs and which works and which doesn’t, so can you upload them somewhere separately? Rtcamp.com layout is not the clearest one either.

Wildcard DNS entries must be of type CNAME, otherwise they won’t work as expected.

Actually, it is advised to use separate A-records in case you want working Let’s Encrypt SSL for the mapped domain and also needed if you want to use Cloudflare Proxy to work on these mapped domains. Cloudflare doesn’t support CDN for wildcard entries.

There are two working config,

  1. This is presspeace.com file under /etc/nginx/sites-available . In this case, I don’t need any other ssl.conf or force-ssl.conf file.

https://pastebin.com/41TtRMRG

  1. This configuration needed a presspeace.com conf file under etc/nginx/sites-available and ssl.conf under /var/www/presspeace.com/conf/nginx

presspeace.com file here:

https://pastebin.com/3qkNksLX

ssl.conf here:

https://pastebin.com/7bbpdTLD

Both config is working for all mapped domains but it always open non-www version ( ie. https://domain.com ) Few issues that I am facing with this config.

Sudomains redirects to the primary domain but the mapped domain to these sub-domains work correctly. Unable to open the network admin page ( or say unable to install new plugins) as the network link: https://domain.com/wp-admin/network/plugins.php redirects to the home page. ir. https://domain.com

And the main moto of this discussion - non-www page doesn’t redirect to www-page.

Give it a look if possible.[quote=“portofacil, post:13, topic:8799, full:true”] Wildcard DNS entries must be of type CNAME, otherwise they won’t work as expected. [/quote]

Well, as per various tutorials, it shouldn’t impact and A-records are more preferable way to do wildcard entries ( since few providers like Godaddy doesn’t support CNAME wildcard entries) but I still tried it and changed wildcard entries from A-records to CNAME on Cloudflare. It’s working but couldn’t bring any positive or negative change to what I was getting before.

Thank you.

I’ll investigate. In the meanwhile, have your read this : https://www.digitalocean.com/community/questions/configuring-multiple-domains-and-subdomains-dns-nginx-issues-abound ?

Not particularly this write-up. Let me go through it. Thank you.

Though, Please do a proper investigation whenever you have little extra time. I will keep you all updated here if I found any solution.

Thank you.

Oh, yeap, i missed your previous post since i was taking a look at the original post.

Well, in your case, I would modify let the server_name directive as it is now and I would focus on the database changes (via Search-Replace software found here https://interconnectit.com/products/search-and-replace-for-wordpress-databases/ ), after removing custom rewrites of:

if ( $http_host ~* "(?!www\.).") {
             rewrite ^ https://www.$host$request_uri permanent;

and

if ($scheme = http) {
return 301 https://$host$request_uri;
}

Hey @ggloveswp,
Thistime, I deleted the old site. Reinstall wordpress multisite and configure it as you have mentioned.

All is okay but mainly two issues, now it doesn’t force SSL and hence redirects to non-https version of the page.

I ran search and replace plugin for primary domain presspeace.com to check but after doing this, I was unable to access wordpress dashboard.

It gives, 403 Forbidden error like this:

The current cofigurations for your quick info:

/etc/nginx/sites-available/presspeace.com : https://pastebin.com/ujUBLbWM
/var/www/presspeace.com/conf/nginx/ssl.conf : https://pastebin.com/20eAGdWL

Your further investigation needed :slight_smile:

Thank you.

Sounds like you replaced something in a wrong way. What does errorlog says? Is restoring a previous db backup an option? You should replaced only http:// to https://

Yes, I did exactly the same. Search and replace http://www.domain.com with https://www.domain.com

This time I had installed with ‘www’ in the domain name:

sudo ee site create www.domain.com --wpsubdom --wpfc

One thing to notice is that I see proper URL ie. (https://www.domain.com/wp-login.php ) after search and replace. I don’t what is causing this 403 error.

No I don’t have any backup. This is totally an experimental installation, and hence I have nothing to worry. I will change https:// to https by accessing PHPMyAdmin and will let you know.