-1

I want to redirect my non-www to www . SSL working fine and both url working fine with ssl. https://example.com https://www.example.com both working but I want to redirect https://example.com to https://www.example.com

I am working with lamp-server in AWS ec2 and using certbot for ssl.

My apache config.

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html

    <Directory /var/www/html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
    </IfModule>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

I tried many online tutorials but nothing helped, Thanks in advance for any help or support.

Devid John
  • 23
  • 7
  • "My apache config." - You've only posted _part_ of your server config, ie. the vHost for port 80 (HTTP). To redirect from `https://example.com` (HTTPS) to `https://www.example.com` as stated in your question then you need to locate your vHost for port 443 (HTTPS), ie. ``. – MrWhite Feb 18 '22 at 01:44

1 Answers1

1

Try (note: Apache may throw errors if the comments starting with # are not removed):

# turns on the rewrite engine
RewriteEngine On
# checks if domain is not www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
# redirects to www.example.com
RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# the above is enough for 443 VirtualHost

# checks if https is not on
RewriteCond %{HTTPS} !on
# redirects to https on the same domain
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

So, your full configuration:

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html

    <Directory /var/www/html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
    </IfModule>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{HTTPS} !on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

Add the following to the <VirtualHost *:443> configuration:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^ https://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Example person
  • 3,198
  • 3
  • 18
  • 45
  • "`RewriteCond %{HTTPS} !on`" - there's no need to check that `HTTPS` is not `on` in the vHost for port 80 (ie. HTTP). Although by extension, this doesn't actually answer the question since it doesn't redirect `https://example.com` (note HTTPS) - because it's in the vHost for port 80. (All the vHost:80 needs to do is redirect _everything_ to HTTPS+canonical host, ie. `Redirect 301 / https://www.example.com/`.) – MrWhite Feb 18 '22 at 01:30
  • Apache doesn't support line-end comments, so your first code block (with line-end comments) would actually trigger a 500 error if used as written, because of the 4th directive (ie. `RewriteCond %{HTTPS} !on # checks if https is not on`) - "bad flag delimiters". – MrWhite Feb 18 '22 at 01:33
  • (This question has appeared in the close-votes review queue.) – MrWhite Feb 18 '22 at 01:34
  • @MrWhite "*(This question has appeared in the close-votes review queue.)*", does that mean this question is going to be deleted? – Example person Feb 18 '22 at 05:51
  • @MrWhite this was an old answer. While posting the answer I did not have the current knowledge. I will edit the answer accordingly – Example person Feb 18 '22 at 05:55
  • "*there's no need to check that HTTPS is not on in the vHost for port 80 (ie. HTTP)*", well, some day, they can set `SSLEngine Optional`, and some clients will get to upgrade to TLS. I am not going to remove that since they can also in future move that block to the global configuration. Anyways, the performance issue from that is not really a lot, since a lot of browsers do automatically request with https:// (Anyways, this is just usable name for HTTP-over-TLS) – Example person Feb 18 '22 at 05:55
  • @MrWhite I have hopefully fixed all the issues. Please let me know about any other. – Example person Feb 18 '22 at 06:09