简体   繁体   中英

Apache2 Force redirect to HTTPS doesn't work

I'm trying to set redirect of all trafic to HTTPS. But when I access http://example.com it gives "403 Forbidden" error. With HTTPS everything works good.

My /etc/apache2/apache2.conf has everything by default except of:

<Directory /home/site/www>
        Options -Indexes
        AllowOverride All
        Require all granted
</Directory>

My /etc/apache2/sites-available/example.com.conf looks like this:

<VirtualHost *:443>
        ServerAdmin admin@example.com
        DocumentRoot /home/site/www
        ServerName example.com
        ServerAlias www.example.com

        SSLEngine on
        SSLCertificateFile /home/site/.ssl/example_com.crt
        SSLCertificateKeyFile /home/site/.ssl/example.key
        SSLCertificateChainFile /home/site/.ssl/example_com.ca-bundle

        ErrorLog /home/site/www/errors.php
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

My .htaccess looks like this:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

The website is hosted on Ubuntu 16.04. on Google Cloud. The same setup but on Amazon AWS works good and rewrites all requests to HTTPS.

Solved.

Just had to sudo a2dissite 000-default.conf and add:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot /home/site/www
</VirtualHost>

to /etc/apache2/sites-available/example.com.conf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM