0

Hi I am a web designer and have recently starting maintaining a VPS server
I am using a Lamp VPS server running ubuntu 18 with multiple PHP websites
have edited virtual host and htaccess multiple times.

Problem :

1.in all websites: all "www version of websites" redirecting to apache homepage
2.non-https version not redirecting to https version

Should I reissue ssl cert for www version seperately ?

Code for Virtual Hosts

<VirtualHost *:80>
    ServerName www.temp.com
    ServerAlias temp.com
    Redirect "/" "https://www.temp.com"
    DocumentRoot /var/www/temp.com/

    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,END]

    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,END]
   </VirtualHost>

Code for .htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>

I am using Certbot for SSL certificates using


sudo certbot --apache

1

1 Answer 1

0
<VirtualHost *:80>

ˆˆˆ A VirtualHost on port 80 should always be the plain, clear text, http host. You can never have both plain http AND https on the same port.
That means that the configuration lines below can NEVER apply and should be omitted:

    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,END]

This Redirect directive:

   Redirect "/" "https://www.temp.com"

is the simple and recommended version of the mod-rewrite code below that is functionally equivalent:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,END]

they both redirect from plain http to https. You don't need both. I recommend deleting the mod_rewrite code.

You certainly don't need to repeat that redirect to https functionality a third time in your .htaccess file.


There should be a code block for a HTTPS VirtualHost that looks something like:

<VirtualHost *:443>
    ServerName www.temp.com
    ServerAlias temp.com
    DocumentRoot /var/www/temp.com/

    SSLEngine on
    SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1

    SSLCertificateFile /etc/letsencrypt/live/.../cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/..../privkey.pem
    SSLCACertificateFile /etc/letsencrypt/live/.../chain.pem

</VirtualHost>

1
  • Where should i add the <VirtualHost *:443> block in sites-available/website.conf
    – SACHIN HD
    Jul 4 at 18:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .