0

I need your help guys.

I have a server running Apache 2 and in there I've multiple vhosts bound to different domains. I want that the default vhost (so every address not bounded with a vhost) redirects to one of this domain, call it maindomain.com. Previously, I put in the default vhost config a redirect to maindomain.com and everything was working fine.

The problem is that now I changed the hostname of the system (I had to do that!) to maindomain.com. So, if I understood correctly, now maindomain.com is implicitly the default domain (infact I fell in a redirect loop issue).

I fixed this issue by setting maindomain.com location as default vhost. Things work but I get no redirect. If you, for example, visit www.maindomain.com (or any address not bound with a vhost) you visualize the correct site but in address bar you still see www.maindomain.com and NOT maindomain.com.

So, how I can fix this avoiding redirect loops?

  1. Is it possible to disable the fact that the hostname behaves like default vhost in Apache?
  2. Or, alternatively, how I can display maindomain.com in the address bar when someone connects using another address?

I hope that you can help me out, thanks in advance.

1 Answer 1

0

As you know, if a request comes in for a name for which there is no explicit VirtualHost, then Apache will use the default VirtualHost to handle the request. So, you're not seeing a redirect for URLs that don't have their own VirtualHost because you changed the default from the one that redirects to the one that just serves the content.

If you know which domains you're going to be handling, you should use wildcards to catch unexpected subdomains, e.g.:

<VirtualHost *:80> ServerName *.maindomain.com ServerAlias otherdomain.com *.otherdomain.com RedirectPermanent / http://maindomain.com/ </VirtualHost> <VirtualHost *:80> ServerName maindomain.com ... rest of VirtualHost ... </VirtualHost> 

I'm not sure how this differs from what you had in the first place, though.

3
  • Wildcards can be a possible (partial) workaround. But the problem is that Apache recognize as default the domain that matchs the machine's hostname Commented Nov 24, 2013 at 9:35
  • I don't think that's true. The Apache docs say (emphasis mine): "If the request contained an unknown or no Host: header it is always served from the primary name-based vhost (the vhost for that address/port appearing first in the configuration file)". Check the order of your VHosts? Commented Nov 25, 2013 at 11:30
  • Well, the configuration was working fine until I changed the hostname. After I changed hostname to maindomain.com apache didn't recognize the vhost associated with maindomain.com. Vhosts order is correct (was working before!). By the way I partially solved using wildcards as you suggested Commented Nov 25, 2013 at 15:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.