skip to Main Content

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
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.

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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 http://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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search