-1

I have a website that I can access with a number of different URLs entered into the web browser. All these cases works as well (will be loaded through https protcol):

lamtakam.com
https://lamtakam.com
https://www.lamtakam.com
http://lamtakam.com       -- automatically will be redirected to https which is correct

Ok all fine by now. The only problem is this URL:

http://www.lamtakam.com

It will be loaded through http (not https) protocol. How can I make it redirect to https protocol too?


My server uses Linux ubuntu as its OS and apache as web server.


EDIT: Here is the content of /etc/apache2/sites-available/000-default.conf file:

    #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/myweb

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =lamtakam.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

    <Directory /var/www/html/myweb>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
Martin AJ
  • 191

1 Answers1

1

The issue is in your rewrite condition on when to do the rewrite. It is set to only match the bare domain exactly (= lamtakam.com), and not match the www. subdomain in the condition upon which to rewrite.

Try using this instead for your rewrite conditions and the rule:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?lamtakam\.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [END,NE,R=permanent]

... then restart Apache and see if the site redirection now occurs. Take care to burn your browser cache first, though, so that you don't have a problem with cached page data from before.

This breaks from what your config was set up to do by:

  1. Requiring that HTTPS not be on (i.e. the request was http:// and not https://), and
  2. Requiring the requested host name / domain matches the specified regex (which matches both www.lamtakam.com and lamtakam.com), and
  3. Does the rewrite using the value of HTTP_HOST instead of SERVER_NAME, to keep the domains identical. You can change this to be %{SERVER_NAME} in the rewrite rule, if you wish (or if your site needs this), though I prefer to use the initially requested hostname in HTTP_HOST.

Note that I suggest using %{HTTP_HOST} so that you match the actual requested hostname, rather than the 'server name' that Apache stores.

(this answer is adapted from an answer on RewriteCond %{SERVER_NAME} syntax by starkeen over on StackOverflow)

Thomas Ward
  • 78,878