Redirecting non-www to www not working

Keywords: LAMP/MAMP/WAMP - AWS - Technical issue - Other
bnsupport ID: 3b8ccfca-4e8b-1ef4-b1f8-e9712995a095
Description:
Hi, I am trying to force https and redirect non-www to www for my site ( LAMP on LightSail ).
I tried with this approach the official documentation but is not working.

It is redirecting to https but it is not redirecting non-www to www domain.

Here is the content of “/opt/bitnami/apache/conf/bitnami/bitnami.conf” :

# Default Virtual Host configuration.

# Let Apache know we're behind a SSL reverse proxy
SetEnvIf X-Forwarded-Proto https HTTPS=on

<VirtualHost _default_:80>
  DocumentRoot "/opt/bitnami/apache/htdocs"

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^(localhost|127.0.0.1)
    RewriteRule ^(.*)$ https://www.mydomain.org$1 [R=permanent,L]



  <Directory "/opt/bitnami/apache/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>

  # Error Documents
  ErrorDocument 503 /503.html
</VirtualHost>

Include "/opt/bitnami/apache/conf/bitnami/bitnami-ssl.conf"

Here is the content of: “/opt/bitnami/apache/conf/bitnami/bitnami-ssl.conf”

<VirtualHost _default_:443>
  DocumentRoot "/opt/bitnami/apache/htdocs"
  SSLEngine on
  SSLCertificateFile "/opt/bitnami/apache/conf/bitnami/certs/server.crt"
  SSLCertificateKeyFile "/opt/bitnami/apache/conf/bitnami/certs/server.key"


    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.mydomain.org$
    RewriteCond %{HTTP_HOST} !^(localhost|127.0.0.1)
    RewriteRule ^(.*)$ https://www.mydomain.org.com$1 [R=permanent,L]




  <Directory "/opt/bitnami/apache/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>

  # Error Documents
  ErrorDocument 503 /503.html
</VirtualHost>

Please help.

Hi @arunp,

You are not redirected from the non-www domain to the www one because the non-www domain is not configured properly.

https://www.whatsmydns.net/

Please ensure both www and non-www domains are configured with the instance’s IP address :slight_smile:

Happy to help!


Was my answer helpful? Click on :heart:

1 Like

Thank you very much for the solution. That made it work. I wonder why this is not mentioned anywhere in the documentation about using a single domain.

By the way, is it possible to redirect non-www ( mydomain.org) to www ( www.mydomain.org) using just the dns entry ?

Thanks a lot for your help. You are amazing !

Hi @arunp,

Could you please elaborate? Do you want to redirect to the www domain only if the user is requesting the non-www domain? I think this guide will be useful for you :slight_smile:

https://docs.bitnami.com/aws/infrastructure/lamp/administration/use-single-domain/

Happy to help!


Was my answer helpful? Click on :heart:

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