Question

I would like to set up my virtual hosts to do the following. For background, before I had access to virtual hosts, I did this on another server in .htaccess using entirely rewrite. As you can assume, this was extremely slow.

  • Requests to example.com unchanged
  • Requests to www.example.com permanantly redirected to example.com
  • Requests to existing subdomain (a.example.com or b.example.com) unchanged, deliver their own content
  • Requests to non-existing subdomain redirected to example.com

vhosts.conf

<VirtualHost *:80>
    ServerName example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName a.example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName b.example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    Redirect permanent / http://example.com
</VirtualHost>

Issue 1

In this case, for some reason b.example.com matches, but a.example.com redirects to example.com. That seems odd.

Issue 2

rstlne.example.com displays the contents of example.com, but address still shows rstlne.example.com. That is expected, but I need to know how to redirect this case to example.com. The only thing that comes to mind about redirecting non-existent domains would be to have the first (default) virtual host have the redirect, but in that case example.com would match and be a redirect loop.

Was it helpful?

Solution

Oh wow. It turns out I had this extremely close to correct. Revision below is working correctly. My primary problems were:

  1. :e scp://<shortname>//etc/httpd/conf/vhosts/vh.conf in vim, although it says the console returned 1, doesn't actually update files that need sudoedit privileges. Total boneheaded mistake. Using sudoedit or similar in ssh console was needed to actually make modifications that need root.
  2. I had no idea that browser cache played a role in remembering redirection, so things just weren't behaving properly.

I am leaving this with information in case anybody else needs to do similar.

vhosts.conf

<VirtualHost *:80>
    ServerName example.com
    ...
</VirtualHost>

<VirtualHost *:80>
    ServerName a.example.com
    ...
</VirtualHost>

<VirtualHost *:80>
    ServerName b.example.com
    ...
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias *.example.com
    Redirect permanent / http://example.com
</VirtualHost>

Issue 1 Solution

Clearing my browser cache solved this issue. It actually works as expected.

Issue 2 Solution

Match a CNAME or A record wildcard domain in the last VirtualHost that does the redirect. It must be last because the VirtualHosts are matched in order. So any subdomains that have not already matched (exist), will be caught by the wildcard and like the www be redirected.

I still have some reservations about doing the wildcard match permanently due to the possible addition of subdomains later, but the effect of this may be minimal or nothing to SEO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top