5

I have hosted an website on a VPS using Apache Virtual Server, as I plan to host a few more sites on that VPS later on. The virtual host was working properly until I installed SSL on it using the LetsEncrypt Certbot. After doing that, https://techkernel.org works fine, but the http variant, http://techkernel.org gets redirected to the default Apache webpage. The virtual host configurations aren't working properly and it's getting directed to the default /var/www/html directory.

If you could tell me how to fix it so that both HTTP and HTTPS requests get redirected to HTTPS and also show the correct website, that would be much helpful. Thank you.

Relevant information:

etc/apache2/sites-available/techkernel.org.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName www.techkernel.org

    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www/techkernel.org/public"

    ServerAlias techkernel.org

    # 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} =www.techkernel.org [OR]
    RewriteCond %{SERVER_NAME} =techkernel.org
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

</VirtualHost>

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

var/www/techkernel.org/public/.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

ls -l /etc/apache2/sites-enabled

lrwxrwxrwx 1 root root 35 Dec 28 11:08 000-default.conf -> ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 38 Jan 21 04:31 techkernel.org.conf -> ../sites-available/techkernel.org.conf
lrwxrwxrwx 1 root root 55 Dec 28 19:46 techkernel.org-le-ssl.conf -> /etc/apache2/sites-available/techkernel.org-le-ssl.conf

ls -l /etc/apache2/sites-enabled

-rw-r--r-- 1 root root 1332 Jul  5  2016 000-default.conf                                                                                                           
-rw-r--r-- 1 root root 6437 Aug  7 10:56 default-ssl.conf                                                                                                           
-rw-r--r-- 1 root root 1574 Jan 19 16:05 techkernel.org.conf                                                                                                        
-rw-r--r-- 1 root root 1613 Dec 28 19:46 techkernel.org-le-ssl.conf

3 Answers 3

5

Besides the error you maybe found in your 000-default.conf, the following problems were in your rewrite rules:

RewriteEngine on
RewriteCond %{SERVER_NAME} =www.techkernel.org [OR]
RewriteCond %{SERVER_NAME} =techkernel.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

should be

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?techkernel.org
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

%{SERVER_NAME} could be the value from the VirtualHost config if UseCanonicalName On is set. So it would redirect everything no matter what URI used by the visitor. If this is the intention, then just remove any RewriteCond and use

RewriteEngine on
RewriteRule ^/(.*) https://www.techkernel.org/$1 [NC,R=301,L]

If that is fixed, you could/should remove all entries about this in your .htaccess

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]
2

You've pasted your config as a sample from sites-AVAILABLE. Have you symlinked it to sites-ENABLED to actually, you know, enable it?

edit - sorry, that seemed unnecessarily snippy. My point is that your HTTPS variant is working, and is in a separate config file. Your HTTP is not, which seems to me because you haven't created this symlink and therefore Apache has no config for "what you do when you receive a request on port 80 with the Host: header set to techkernel.org", therefore it's falling through to the default site. Your config otherwise looks perfectly good to me, it's just not actually being used by Apache.

8
  • I'm new to these. How can I create the symlink? Jan 20, 2017 at 9:19
  • @SkullTech a2ensite techkernel.org
    – scampbell
    Jan 20, 2017 at 12:47
  • @scampbell That doesn't seem to work. First it said Site techkernel.org already enabled. So I a2dissite'd it and then a2ensite'd it again. And reloaded apache2. But still no result. Jan 21, 2017 at 4:36
  • edit - UGH, I can't rememeber how SO formatting works... Sounds to me like you have two different files, not symlinks. What does "ls -l /etc/apache2/sites-{available,enabled}" say? It sounds like you have two files, both called "techkernel.org.conf", one in available (which is configured for HTTP) and one in enabled (which is configured for HTTPS). Therefore a2ensite THINKS that you're trying to enable a config which is already enabled.
    – gac
    Jan 21, 2017 at 8:54
  • What I'd probably do (except I ran out of space in that comment) is: ` mv /etc/apache2/sites-enabled/techkernel.org.conf /etc/apache2/sites-available/HTTPS_techkernel.org.conf mv /etc/apache2/sites-available/techkernel.org.conf /etc/apache2/sites-available/HTTP_techkernel.org.conf a2ensite HTTP_techkernel.org a2ensite HTTPS_techkernel.org service apache2 reload ` The system used by Apache is pretty much designed so that {site,conf,mods}-available contain all the real files with the settings in, then these are symlinked over to -enabled to turn them on
    – gac
    Jan 21, 2017 at 8:59
2

Well it turned out that the 000-default.conf was messing up. And it came to my notice only after running the apachectl -S command. After fixing it up (entering proper ServerName and DocumentRoot and also some URL rewrite conditions) and reloading apache2, it works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.