-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
  • 181
  • 2
  • 9
  • 1
    Downvoter please leave a comment and explain what's wrong with my question? – Martin AJ Aug 09 '18 at 16:23
  • I see now. Did you Google [redirect http to https on Apache](https://www.google.com/search?q=redirect++http+to+https+on+Apache) and try a few of the how to's and work throughs that show up? – zwets Aug 09 '18 at 16:33
  • @zwets You know, the redirection happens as well on everything except `www` subdomain. As you can see in examples, `http://lamtakam.com` will be redirected to `https://lamtakam.com` as well. But the only problem is `http://www.lamtakam.com` which will not be redirected to `https`. What should I search for this issue ? – Martin AJ Aug 09 '18 at 16:36
  • 1
    Possible duplicate of [HTTP to HTTPS rewrite rule not working](https://askubuntu.com/questions/550204/http-to-https-rewrite-rule-not-working) – Thomas Aug 11 '18 at 08:37

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
  • 72,494
  • 30
  • 173
  • 237
  • I replaced your `rewriteCond`s and `rewriteRule` and also restarted *apache2* .. but still only this `http://www.lamtakam.com` will not be redirected to `https` protocol `:-(` – Martin AJ Aug 09 '18 at 17:22
  • You restarted Apache after applying these changes, correct? – Thomas Ward Aug 09 '18 at 17:24
  • yes .. using this `service apache2 restart` – Martin AJ Aug 09 '18 at 17:25
  • Oh yes .. it worked thank you .. `RewriteEngine on` was removed by mistake – Martin AJ Aug 09 '18 at 17:36
  • 1
    @MartinAJ ah, I knew it would be something stupid like that (I didn't put `RewriteEngine on` in my answer because I only specified to edit the conditions and the rule). Glad to hear that this helped solve your problem! :) – Thomas Ward Aug 09 '18 at 17:37
  • You're the first SE moderator I like his behavior `:-)` – Martin AJ Aug 09 '18 at 17:37
  • Ok bro, recently I've noticed when I use `www` in the URL, it will be redirected to the **not secure** version of the `https` protocol. My apache configuration is exactly the same as what you said in your answer. Any idea what should I do? [see, the redirection happens, but not secure](https://www.lamtakam.com/), and without `www` subdomain, the redirection [happens well](http://lamtakam.com) – Martin AJ Aug 21 '18 at 23:04
  • Ok my problem has been resolved. – Martin AJ Aug 21 '18 at 23:25