0

I want to setup my Apache 2.4 server in a way to redirect all requests in following way:

http://domainname.tld/ to http://www.domainname.tld/

and

https://domainname.tld/ to https://www.domainname.tld/

my vhost conf file is as follows:

<VirtualHost *:80>

    ServerName www.domainname.tld

    ServerAlias www.domainname.tld

    DocumentRoot ......

    # other vhost settings
    # ...
    # ...
    # ...

</VirtualHost>

<VirtualHost *:80>
    ServerName domainname.tld
    ServerAlias domainname.tld
    Redirect "/" "http://www.domainname.tld/"
</VirtualHost>

<VirtualHost *:443>

    ServerName www.domainname.tld

    ServerAlias www.domainname.tld

    DocumentRoot /..............

    # SSL configuration
    # ...
    # ...
    # ...

</VirtualHost>

<VirtualHost *:443>
    ServerName domainname.tld
    ServerAlias domainname.tld
    Redirect "/" "https://www.domainname.tld/"
</VirtualHost>

I don't have any rewrite rules in my htaccess file.

http redirect works ok, but if i open https://domainname.tld my request is redirected to the very first vhost configuration on my server.

Please can you tell me what's wrong with my vhost configuration?

As far as I know the best way to achieve this type of redirect is to modify the vhost configuration instead of adding rewrite rules to the htaccess file. The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).

Please share your comments! Thank you in advance!

  • 1
    Possible duplicate of [htaccess redirect for non-www both http and https](http://stackoverflow.com/questions/2015159/htaccess-redirect-for-non-www-both-http-and-https) – Sumurai8 Dec 14 '15 at 16:51

2 Answers2

1

Your Virtual Hosts have redundant entries, so you need to try and clean them up. You only use ServerAlias for other domain names you want to include. Also you should use RedirectMatch since you want to use mod_alias instead of mod_rewrite because it won't match the URI after the hostname if not.

Try your config this way.

<VirtualHost *:80>
    ServerName domainname.tld
    RedirectMatch 301 "(.*)$" "http://www.domainname.tld$1"
</VirtualHost>

<VirtualHost *:80>
    ServerName www.domainname.tld
    DocumentRoot ......

    # other vhost settings
    # ...
    # ...
    # ...
</VirtualHost>

<VirtualHost *:443>
    ServerName domainname.tld
    RedirectMatch 301 "(.*)$" "https://www.domainname.tld$1"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.domainname.tld
    DocumentRoot /..............
    # SSL configuration
    # ...
    # ...
    # ...
</VirtualHost>

Make sure you restart Apache after changes and that you have a valid cert. And that Apache is also listening on port 443.

Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • Hello, thank you for your detailed answer. I changed my vhsot conf according to your explanation, HTTP flow works fine, but HTTPS flow again falls back to my first SSL vhost, which is for another domain. Maybe I'l try https redirect to www with some rewrite rule or with a code change in PHP. – Velizar Nenov Dec 14 '15 at 19:35
  • So you have multiple 443 vhosts other than what's shown? – Panama Jack Dec 14 '15 at 19:49
  • Yes, that's right - I have multiple vhosts configured on my server. – Velizar Nenov Dec 14 '15 at 20:29
  • Something else might be conflicting. Can you post your others? – Panama Jack Dec 14 '15 at 20:55
  • This is the conf file for my very first vhost where my requests are redirected in case of https://domainname.tld. (non-www) ... http://pastebin.com/5JBZxGDn Do you think it can be a browser issue? – Velizar Nenov Dec 15 '15 at 05:51
0

This article explains the solution for my question:

How To Avoid SSL Certificate Mismatch Errors When Redirecting Multiple Virtual Hosts - Because the SSL protocol encapsulates HTTP traffic, adding or removing the 'www' subdomain must be done correctly.

https://www.tombrossman.com/blog/2014/avoid-certificate-mismatch-when-redirecting-multiple-ssl-virtualhosts/

<VirtualHost :443>
    ServerName EXAMPLE-1.com
    SSLEngine on
    SSLCertificateFile      /PATH/TO/CERT.crt
    SSLCertificateChainFile /PATH/TO/CHAIN/FILE.pem
    SSLCertificateKeyFile   /PATH/TO/KEY.key
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>

<VirtualHost :443>
    ServerName WWW.EXAMPLE-1.com
    ...
</VirtualHost>

All you need to do is add the certificate paths inside the redirect block so the key exchange can take place prior to the redirect.