Force nonwww domain name

Keywords: WordPress + NGINX + SSL - AWS - Technical issue - Other
Description:
I just started a new bitnami server on ec2 using wordpress/nginx/mariadb and your most recent file structure. Under this new structure it seems I am still able to force https inside ‘/opt/bitnami/nginx/conf/server_blocks/wordpress-server-block.conf’ but it seems I am unable to force nonwww using my old ways.

Any ideas?

Thanks

Hi @rob83,

We have a Support Tool that will gather relevant information for us to analyze your configuration and logs. Could you please execute it on the machine where the stack is running by following the steps described in the guide below?

Please note that you need to paste the code ID that is shown at the end.

Can you also check if using this guide helps you?

https://docs.bitnami.com/bch/apps/wordpress-pro/administration/use-single-domain-nginx/

That page was the way I used to do it, but I am using bitnami’s latest EC2/Nginx server which does not appear to have the file /opt/bitnami/nginx/conf/bitnami/bitnami.conf anymore.

With the newer setup I am able to force https by putting the old code into “/opt/bitnami/nginx/conf/server_blocks/wordpress-server-block.conf”

But I am unable to force nonwww by adding the following code into that file or /opt/bitnami/nginx/conf/server_blocks/wordpress-https-server-block.conf

if ($host != "example.com") {
         return 301 https://example.com$request_uri;

Anyways, here’s the support tool code: 91814f61-05b1-b363-6f58-1c5304d775c4

Thanks :slight_smile:

Hi @rob83,

Thanks for your message. I reproduced your issue and I think I have found a solution. First, can you tell us if you have a valid SSL certificate for your domain?

I launched a fresh new server and I added the next configuration in both files (wordpress-server-block.conf and wordpress-https-server-block.conf), where mydomain.com is a valid domain (notice example.com is an special case).

    if ($host != "mydomain.com") {
      return 301 https://mydomain.com$request_uri;
    }

I also forced the HTTPS redirection in the wordpress-server-block.conf file by placing the next line under the block above

return 301 https://$host$request_uri;

After that, I restarted NGINX and did some curl testing.

$ curl -LI "http://127.0.0.1"
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 22 Jun 2021 10:42:21 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://mydomain.com/
X-Frame-Options: SAMEORIGIN

curl: (60) SSL certificate problem: self signed certificate
More details here: https://curl.haxx.se/docs/sslcerts.html

The error message is due to the self-signed certificate it is using. If I add the -k option of curl to avoid SSL certificate validations, I get

$ curl -kLI "http://127.0.0.1"
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 22 Jun 2021 10:43:27 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://mydomain.com/
X-Frame-Options: SAMEORIGIN

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 22 Jun 2021 10:43:27 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.20
Link: <https://mydomain.com/wp-json/>; rel="https://api.w.org/"
X-Frame-Options: SAMEORIGIN

Other tests I performed

  • domain with HTTP
$ curl -kLI "http://mydomain.com"
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 22 Jun 2021 10:46:15 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://mydomain.com/
X-Frame-Options: SAMEORIGIN

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 22 Jun 2021 10:46:15 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.20
Link: <https://mydomain.com/wp-json/>; rel="https://api.w.org/"
X-Frame-Options: SAMEORIGIN
  • www-domain with HTTP
$ curl -kLI "http://www.mydomain.com"
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 22 Jun 2021 10:47:50 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://mydomain.com/
X-Frame-Options: SAMEORIGIN

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 22 Jun 2021 10:47:50 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.20
Link: <https://mydomain.com/wp-json/>; rel="https://api.w.org/"
X-Frame-Options: SAMEORIGIN
  • domain with HTTPS
$ curl -kLI "https://mydomain.com"
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 22 Jun 2021 10:48:50 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.20
Link: <https://mydomain.com/wp-json/>; rel="https://api.w.org/"
X-Frame-Options: SAMEORIGIN
  • www-domain with HTTPS
X-Powered-By: PHP/7.4.20
$ curl -kLI "https://www.mydomain.com"
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 22 Jun 2021 10:49:24 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://mydomain.com/
X-Frame-Options: SAMEORIGIN

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 22 Jun 2021 10:49:24 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.20
Link: <https://mydomain.com/wp-json/>; rel="https://api.w.org/"
X-Frame-Options: SAMEORIGIN

Can you check if it works on your side too? I think the SSL certificate is the key here

Hey @gongomgra,

Thanks for the response. I made a new server and setup letsencrypt using the documentation provided here as usual (nginx approach A). So I am not sure if or why there would be any SSL errors.

https://docs.bitnami.com/bch/how-to/generate-install-lets-encrypt-ssl/#step-1-install-the-lego-client

Other than that there really isn’t any other server file modifications besides my edit inside wordpress-server-block.conf

It seems to work now. I did it in the order you mentioned, both under the “server_name” line and all appears well now.

I appreciate your help, @gongomgra you guys are great.

I hate to admit but after all of that I think I was missing a bracket }

…I do dumb stuff like this all the time, I’m sorry.

Topic can be closed.

Hi @rob83,

Thanks for the info. I’m glad you fixed your issue! We will close this thread as solved. Please do not hesitate to open a new one with any other questions you may have.