I have an issue redirecting from the http site to the https site.
This index.php file is in the root folder, redirecting to a suburl:
<?php
header("Location: /news/");
?>
the url https://www.example.com
is correctly redirecting to https://www.example.com/news/
the url https://www.example.com/index.php
is correctly redirecting to https://www.example.com/news/
the url http://www.example.com/index.php
is correctly redirecting to https://www.example.com/news/
however the url http://www.example.com
is showing an empty directory, as in the image below:
these are the config files:
/etc/apache2/sites-available/www.example.com.conf
<VirtualHost *:*>
ServerName www.example.com
DocumentRoot /var/www/vhosts/example.com/httpdocs
ServerAlias example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
/etc/apache2/sites-available/www.example.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/vhosts/example.com/httpdocs
ServerAlias example.com
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
How can I correctly redirect from http://www.example.com
to https://www.example.com/news/
?
2
Answers
I used:
Type the code into the conf file.
One has two options — Virtual Host Redirection, and Request Rewriting. Adapt the following configuration fragments to your needs.
Virtual Host Redirection
To be prefered as being simpler and safer. Redirect an HTTP virtual host (Port 80) to a HTTPS virtual host (Port 443) via the virtual hosts configuration:
Request Rewriting
Alternatively — usually, when the virtual hosts config is not accessible — one can edit the
.htaccess
file (create it, if necessary) to rewrite HTTP to HTTPS requests. For that the module mod_rewrite has to be enabled, soin
httpd.conf
has to be set (and usually is, by default). Then add to the.htaccess
file: