3

System: Ubuntu 14.04 , Apache 2.4.7

I want all requests to http://domain.com and https://domain.com be redirected to https://domain.com. I also want to redirect requests to the "www" subdomain to the main domain.com host, whilst a few other subdomains (tools.domain.com and phpmyadmin.domain.com) will stay accessible over http for the moment.

All this should be done directly via the virtualhost config file for domain.com, (/etc/apache2/sites-available/domain.com.conf full content here below), then restarted the apache2 service several times.

Redirection from http://domain.com to https://domain.com works. Redirection from http://www.domain.com to https://domain.com does not work. Redirection from https://www.domain.com to https://domain.com does not work.

Here is the virtualhost file content:

<VirtualHost *:*>
    ServerName www.domain.com
    Redirect permanent / https://domain.com/
</VirtualHost>

<VirtualHost _default_:80>
    ServerName domain.com
    Redirect permanent / https://domain.com/
    LogLevel error
</VirtualHost>

<VirtualHost *:80>
    ServerName tools.domain.com
    DocumentRoot /var/www/domain.com/subdomains/tools/public
</VirtualHost>

<VirtualHost *:80>
    ServerName phpmyadmin.domain.com
    DocumentRoot /usr/share/phpmyadmin
</VirtualHost>

<VirtualHost _default_:443>
    ServerName domain.com

    DocumentRoot /var/www/domain.com/public

    <Directory /var/www/domain.com/public>
        Require all granted
    </Directory>

    # SSL CERTIFICATES
    GnuTLSEnable on
    GnuTLSExportCertificates on
    GnuTLSCacheTimeout 500
    GnuTLSCertificateFile /etc/ssl/certs/domain.com-certificate-125023.crt
    GnuTLSKeyFile         /etc/ssl/private/domain.com.key
    GnuTLSPriorities      NORMAL
</VirtualHost>

Update: only https://www.domain.com does not work

Stupid me: the problem was beyond the server! I had not configured any 'www' subdomain in my DNS zone. I corrected that and now I'm almost there.

7
  • Add a separate logs for redirected sites and look into it. You could also debug a problem with tcpdump/wireshark or FF/Firebug. Does the client browser receive a 301 answer ?
    – drookie
    Feb 4, 2015 at 6:35
  • 1
    I actually found the apache2ctl -S to be quite useful in understanding what Apache "understands" from the virtualhost file.
    – pixeline
    Feb 4, 2015 at 13:38
  • Sorry, I'm doing a lot of tries, and it is not accurate anymore. Will update right away. Do you mean that i cannot specify several vhosts using the same 443 port ? I understand this vhost directive as "any request to any ports for www.domain.com should redirect to domain.com". Is that incorrect, or not the correct way to write it ?
    – pixeline
    Feb 4, 2015 at 16:31
  • Hurray! thanks to your comment, I got it to work: I figured that in fact, I don't need to specify any vhost directive for www.domain.com if i use default to point to where should all unknown requests go. Thank you @AD7six
    – pixeline
    Feb 4, 2015 at 16:46
  • @AD7six feel free to publish your explanation as an answer, so I can credit you.
    – pixeline
    Feb 4, 2015 at 16:48

2 Answers 2

2

_default_ means default

With config like this:

<VirtualHost _default_:80>
    ...
</VirtualHost>

<VirtualHost _default_:443>
    ...
</VirtualHost>

<VirtualHost *:*>        
    ...
</VirtualHost>

The last virtual host is unreachable on ports 80 and 443 as any request on those ports will be processed by the relevant _default_ virtual host. The *:* virtual host probably isn't required at all (are you expecting public users to use more ports ??).

Working example

Therefore to have everything redirect to https://example.com you need, for example:

<VirtualHost *:443>
    ServerName example.com

    # SSL config

    ...
</VirtualHost>

<VirtualHost *:80>
    RewriteEngine On
    RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>

    # SSL config

    RewriteEngine On
    RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
</VirtualHost>

Note the use of a capture-everything rewrite rule, as using redirect only matches one url.

3
  • RedirectMatch would allow regex and keep the setup not dependent on an external module (minor perk). Anyway, I changed my file to follow your working example, and nothing redirects anymore, at the moment. Strange. Investigating...
    – pixeline
    Feb 4, 2015 at 17:57
  • It seems _default_ is only relevant for ip-based virtual host configuration. Here, I'm in a name-based virtual host configuration. This worked for me: <VirtualHost *:443> ServerName www.pixeline.eu RedirectMatch (.*) https://pixeline.eu$1 </VirtualHost> <VirtualHost *:80> ServerName www.pixeline.eu RedirectMatch (.*) https://pixeline.eu$1 </VirtualHost>
    – pixeline
    Feb 4, 2015 at 20:21
  • Interesting I thought they were synonymous.
    – AD7six
    Feb 4, 2015 at 20:31
3

Have you tried to use mod_rewrite? You can accomplish the same with the following mod_rewrite rule.

RewriteCond %{SERVER_PORT} 80 [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
3
  • The Apache documentation does not recommend to use mod_rewrite for such simple redirects. So i'd rather not.
    – pixeline
    Feb 4, 2015 at 14:07
  • 1
    There is a difference between not explicitly recommending, and recommending against.
    – BE77Y
    Feb 4, 2015 at 16:19
  • This answer actually works better for me when I use the conditional rewrite within the main *.443 VirtualHost since my current setup does not allow a ServerName on the SSL configuration. I can now rewrite to the desired ssl subdomain with just the default ssl VirtualHost.
    – phil_ayres
    Sep 22, 2015 at 2:09

You must log in to answer this question.

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