2

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:

enter image description here

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/ ?

Daniele B
  • 19,801
  • 29
  • 115
  • 173

2 Answers2

1

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:

# Listening for HTTP connections
<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    # permanently redirects to the site's HTTPS version
    Redirect permanent / https://www.example.com/
</VirtualHost>

# Listening for HTTPS connections
<VirtualHost _default_:443>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /usr/local/apache2/htdocs
    SSLEngine On

    # Further configurations...
</VirtualHost>

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, so

LoadModule rewrite_module modules/mod_rewrite.so

in httpd.conf has to be set (and usually is, by default). Then add to the .htaccess file:

# Enable rewriting
RewriteEngine On 
# Check for HTTPS, if no, execute next line
RewriteCond %{HTTPS} off
# Redirect to HTTPS with status code 301 (moved permanently)
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [L,R=301]
Krokomot
  • 3,208
  • 2
  • 4
  • 20
0

I used:

<VirtualHost *:80>
        RewriteEngine On
        RewriteCond %{HTTPS} !=on
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</virtualHost>

Type the code into the conf file.

  • Request rewriting should usually work, however -- as far as I am aware of -- virtual host redirection should be prefered when possible, as it doesn't depend on a module (mod_rewrite) and is therefore more straightforward. – Krokomot Dec 02 '22 at 03:44