2

I'm setting up a website on raspberry Pi.

my websitename is like this

abc123.example.co

so the thing is that the "abc123" is random, no idea what will it be next time I start the server.

now, I'd like to re-direct all the traffic that comes to the subdomain to a HTTPS of the same website.

my apache 000-default.config looks like this:

<VirtualHost *:80>
        #ServerName www.example.com
        #ServerAlias *.example.co
        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

now, when it is like that, the user is getting redirected multiple times & the website isn't opening.

now, when I disable it:

<VirtualHost *:80>
        #ServerName www.example.com
        #ServerAlias *.example.co
        #RewriteEngine On
        #RewriteCond %{HTTPS} off
        #RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

then hit sudo service apache2 restart

the websites from http actually are being redirected to https.

I'm pulling my hairs out on this one. tried various stuff and am able to narrow down till here. No idea where to go. Please help.

totally new to linux & all, so please be easy on me. I don't know much.

closed as off-topic by Milliways, Dmitry Grigoryev, Steve Robillard, Jacobm001 May 14 '18 at 0:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – Milliways, Dmitry Grigoryev, Steve Robillard, Jacobm001
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Welcome! Have you read httpd.apache.org/docs/current/ssl ? – Fabian May 7 '18 at 15:18
  • 1
    Help us to better understand. You say, "the websites from http actually are being redirected to https." So what is it that's not working for you? – Octopus May 7 '18 at 15:23
  • yes, I need the traffic to be routed to HTTPS from HTTP. it works ONLY when I enable the file, sudo reload apache2 & then disable it and sudo reload apache2. I know it's weird. any pointers please – TheTourer May 7 '18 at 15:55
1

To redirect the requests to your Apache 2.4 name-based virtual host from HTTP to HTTPS you have many options. This one should help you:

/etc/apache2/sites-available/000-default.config is the default configuration file for HTTP (port 80):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

If you enable mod_ssl by sudo a2enmod ssl you'll find a new configuration file at /etc/apache2/sites-available/default-ssl.conf which contains the default configuration for HTTPS (port 443):

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
        <FilesMatch "\.(cgi|shtml|phtml|php)$">
            SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
            SSLOptions +StdEnvVars
        </Directory>
    </VirtualHost>
</IfModule>

Side note: You can keep both files or merge them into a new single file (i.e. /etc/apache2/sites-available/example.com.conf).

Edit 000-default.config and save it:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
    </IfModule>
</VirtualHost>

Then run a configuration file syntax test: sudo apachectl configtest (for more details: man apachectl). If no error occurs, enable both configurations for your vhost:

sudo a2ensite 000-default.config
sudo a2ensite default-ssl.conf

If mod_rewrite is missing, run sudo a2enmod rewrite.

Then run sudo systemctl reload apache2.service and your site should work. If you test your site, disable HTTP cache in your browser!

  • nope, it's not working. not sure why. the page is just loading, but not actually loading up. not even the apache default page is loading. however with the 192.168.x.x address its working, it's redirecting to HTTPS even through the subdomain link. I'm able to redirect but the page itself is not loading. – TheTourer May 12 '18 at 9:47
  • yes, it's 301 moved permanently. the browser says that the server is redirecting too many times. it's driving me crazier now. – TheTourer May 12 '18 at 10:51
  • okay, when I "hashed" out the loop, the server is redirecting to the secured site. I'm totally baffled now. – TheTourer May 12 '18 at 12:25
  • this one: <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L] </IfModule> so, I start the server without the above lines, the http is not redirecting to https. then I "enable" the if condition or the condition I have in my original post. and then save and restart apache2, the website doesn't open. after I put a "#" in front of the lines & restart apache2, then it's working till next reboot. – TheTourer May 12 '18 at 13:11
  • sadly the chat says "You must have 20 reputation on The Stack Exchange Network to talk here. See the faq." I guess I'm out of chat for a while. – TheTourer May 12 '18 at 13:30

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