Apache Virtual Hosts Non-www not working

I'm setting up a Virtual Hosts file on my CentOS 7 box and I'm having trouble getting my domain to resolve correctly.

Here's what my current /etc/httpd/conf.d/vhost.conf file looks like

NameVirtualHost *:80

<VirtualHost *:80>
   ServerAdmin [email protected]
   ServerName www.domain.com
   ServerAlias domain.com
   DocumentRoot /var/www/html/domain.com/public_html/
   ErrorLog /var/log/httpd/error.log
   CustomLog /var/log/httpd/access.log combined

   RewriteEngine on
   RewriteCond %{SERVER_NAME} =www.domain.com [OR]
   RewriteCond %{SERVER_NAME} =domain.com
   RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

It seems the the correct redirects are happening. For exmaple:

domain.com redirects to https://www.domain.com
and www works fine

BUT

https://domain.com doesn't work
http ://domain.com doesn't work

In fact, if I remove the redirects I have set, domain.com ins't working at all, so it looks like the ServerAlias is broken?

I'm wondering if I need another redirect or is there some other step I'm missing?

4 Replies

It's hard to tell since you haven't said what your domain is, but it sounds like you might not have a DNS record for domain.com.

If you're using the Linode DNS Manager, make sure:

  • You have a zone for domain.com (not www.domain.com)
  • You have an A record with a blank hostname (under "A/AAAA Records" on the page for that zone in the DNS Manager)

Some browsers try to be smart about the URL you've entered and will automatically add www to the front of your domain, which can be nice sometimes but may also hide DNS issues while you're configuring your site. Since you said browsing to domain.com works but http://domain.com doesn't, I suspect that may be what's happening here.

Hmm. It looks like all my DNS settings are configured correctly. My zone is for domain.com and I have A records with both a blank hostname and a hostname of www.domain.com

The only other thing that I can think of right now is that I changed my hostname to grump when initially configuring my server. Would that play any effect?

The system's hostname shouldn't matter as far as I know - Apache will use the hostname if you don't have a ServerName, but you do have one in the snippet you posted.

What do you get if you try requesting both http://domain.com and http://www.domain.com from an external machine? If you have a machine with curl installed, try this:

curl -IL http://domain.com
curl -IL http://www.domain.com

That'll show you exactly where each one is redirecting, if applicable.

Also, do you have a similar virtualhost config for SSL on port 443?

I had my local hosts file configured to point to an old out of date IP address……

domain.com *bad ip address*

I'm so embarrassed. I must have set that up months ago and forgot.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct