0

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 webmaster@domain.com
   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 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?

Also, don't mind the spaces between http and the domain name. StackOverflow made me format it that way.

Marty
  • 289
  • 2
  • 14

2 Answers2

0

As presented, no request to anything https will ever work. Normal, you only have a VirtualHost on port 80. You do have a Listen directive for that port right?

For your redirections. It says: if you ask for http://www.example.com or http://example.com, redirect to https://<WHAT THE USER ASKED FOR>. In essence you are forcing your users to use https all the time, no problem there. But you do not have a VirtualHost on port 443, hence no response.

So:

Listen *:80
<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    ErrorLog  /var/log/httpd/80_error.log
    CustomLog /var/log/httpd/80_access.log combined

    RewriteEngine on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>

Listen *:443
<VirtualHost *:443>
    ServerName www.example.com
    # in case users do directly to https
    ServerAlias example.com    

    DocumentRoot /var/www/html/domain.com/public_html/
    DocumentIndex index.html

    ErrorLog  /var/log/httpd/443_error.log
    CustomLog /var/log/httpd/443_access.log combined

    # SSL CONFIGURATIONS, TODO!
</VirtualHost>
  • In your *:443 VH, you will have to configure certificates and SSL.
  • Your certificates will have to be valid for both www.example.com and example.com to avoid browser complaints.
  • Careful there might be an ssl.conf included file under conf.d that defines some of this. Make sure you only set it once to avoid confusion.
  • No need to define DocumentRoot in *:80 VH since it only redirects and does not respond content to client.

Have fun!

Nic3500
  • 8,144
  • 10
  • 29
  • 40
0

I solved the issue. 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.

Marty
  • 289
  • 2
  • 14