Website is not redirecting from non-www to www

Keywords: Django - Google Cloud Platform - Technical issue - Secure Connections (SSL/HTTPS)

bnsupport ID: cd6b711a-83e7-8387-4ec4-303e3b1fdddd

bndiagnostic output:

? Apache: Found possible issues
https://docs.bitnami.com/general/apps/wordpress/troubleshooting/debug-errors-apache/
https://docs.bitnami.com/bch/apps/moodle/troubleshooting/deny-connections-bots-apache/

bndiagnostic failure reason: The suggested guides are not related with my issue

Description:
I have followed the step by step guide by bitnami for installation of letsencrypt ssl on my Django vm instance and also checked for the rewrite rules. Everything is okay, but my website is not redirecting from non-www to www.

Hi @fidrwui

Thanks for using Bitnami Django!

I have been briefly analyzing your configuration and it seems there are several directives in the config files to attempt this redirection. However, they might be interfering with each other. Let’s try to get the right directives:

  • Modify your /opt/bitnami/apache/conf/vhosts/fidrw-https-vhost.conf as follows:
...
<VirtualHost 127.0.0.1:443>
    ServerName fiXXX.com
+  ServerAlias www.fiXXX.com
   ...
  # BEGIN: Enable HTTP to HTTPS redirection
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} !^localhost
  RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
  RewriteCond %{REQUEST_URI} !^/\.well-known
- RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]
+ RewriteRule ^/(.*) https://www.%{SERVER_NAME}/$1 [R,L]
  # END: Enable HTTP to HTTPS redirection
  # BEGIN: Enable non-www to www redirection
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteCond %{HTTP_HOST} !^localhost
  RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
  RewriteCond %{REQUEST_URI} !^/\.well-known
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=permanent,L]
  # END: Enable non-www to www redirection
-   Redirect permanent / http://www.fiXXX.com/
-   ServerAlias www.fiXXX.com
-
-   RewriteEngine On
-   RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
-   RewriteRule ^(.*)$ https://%1$1 [R=permanenet,L]
...
  • Modify your /opt/bitnami/apache/conf/vhosts/fidrw-vhost.conf as follows:
...
<VirtualHost 127.0.0.1:80>
    ServerName fiXXX.com
+   ServerAlias www.fiXXX.com
...
  # BEGIN: Enable HTTP to HTTPS redirection
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} !^localhost
  RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
  RewriteCond %{REQUEST_URI} !^/\.well-known
  RewriteRule ^/(.*) https://www.%{SERVER_NAME}/$1 [R,L]
  # END: Enable HTTP to HTTPS redirection
  # BEGIN: Enable non-www to www redirection
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteCond %{HTTP_HOST} !^localhost
  RewriteCond %{HTTP_HOST} !^[0-9]+.[0-9]+.[0-9]+.[0-9]+(:[0-9]+)?$
  RewriteCond %{REQUEST_URI} !^/\.well-known
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=permanent,L]
  # END: Enable non-www to www redirection
-    Redirect permanent / http://www.fiXXX.com/
-
-    ServerAlias www.fiXXX.com
-
-    RewriteEngine On
-    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
-    RewriteRule ^(.*)$ http://%1$1 [R=permanent,L]
 ...

Notice that I have masked out your domain as fiXXX, you may need to use the proper, uncensored domain

Check that the configuration syntax is okay and restart the service:

$ sudo apachectl -t
$ sudo /opt/bitnami/ctlscript.sh restart apache

Best regards,
Jose Antonio Carmona


Was my answer helpful? Click on :heart:

Hey, can you please check and help me out. It’s important.

Hello @fidrwui,

Have you checked the message of @jcarmona?

Regards

Yes, But it doesn’t help me out…

Ok @fidrwui,

Could you please share a new support output with us so can check the latest status of your files?

Regards

Okay sure.
I will create new thread for that.

Here is my new support ticket

06cdf9dc-6d5e-27e6-12f2-928ce3357fd1

I need a reply soon.

Hello @fidrwui,

I have found several mixed things

In your apache/conf/vhosts/fidrw-https-vhost.conf file

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=permanent,L]

should be

 RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=permanent,L]

Also you ServerName in apache/conf/httpd.conf should be

ServerName www.fiXXX.com:80

Also, in apache/conf/vhosts/fidrw-vhost.conf you have a 2nd virtual host

  <VirtualHost 127.0.0.1:443 _default_:443>
    ServerAlias *
    SSLEngine on
...

I guess it is the one being used. You should not have a 2nd virtualhost, and use apache/conf/vhosts/fidrw-https-vhost.conf for https configuration to keep order. Also, you have a second virtual host for HTTP in apache/conf/vhosts/fidrw-https-vhost.conf that should be in apache/conf/vhosts/fidrw-vhost.conf. Note that using ServerAlias * will match always, and I think you can unify your configuration only with one VirtualHost per file.

You can verify all your virtualHosts with:

apachectl -S

If the suggested changes does not help, please send a new bnsupport output with the latest changes.

Here is the latest support ticket

b34d11fe-a3a4-ec64-887c-1e44cb1f989d

Below is the screenshot for

apachectl -S

Actually, I am new to bitnami servers, so I need expert help for the same.

Hello @fidrwui,

I can see you applied the first 2 changes. However, you did not apply this suggestion.

The idea is to include:

  <VirtualHost 127.0.0.1:443 _default_:443>
    ServerAlias *
    SSLEngine on
    SSLCertificateFile "/opt/bitnami/apache/conf/fidrw.com.crt"
    ...
  </VirtualHost>

inside

<VirtualHost 127.0.0.1:443>
    ServerName fiXXX.com
    ServerAlias www.fiXXX.com
...

Note that you would have 2 ServerAlias, so use only ServerAlias www.fiXXX.com.

And also.

  <VirtualHost 127.0.0.1:80 _default_:80>

    ServerAlias *
    WSGIProcessGroup fiXXX
...
  </VirtualHost>

inside

<VirtualHost 127.0.0.1:80>
    ServerName fiXXX.com

    ServerAlias www.fiXXX.com

...

Please try those changes a let us know.

Best regards

I performed the additional bncert tool after the step you mention. And it’s working fine now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.