Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

httpd redirects with SSL and subdomains/vhosts

I'm trying to setup http -> https redirected subdomains on my website, which is being served by Apache (Version: 2.2.31). I purchased a wildcard SSL certificate that is installed correctly for my main site (www.domain.com) because I get a green lock next to the address, so that part should be done.

The issue: browsing to subdomain.domain.com redirects to www.domain.com and I can't figure out why. I've been reading and following this page as well as several others with similar content, but I'm missing the key ingredient.

The main site is served from /var/www/html and the subdomain is served from /var/www/vhosts/subdomain. Also, I'm getting this error:

[warn] _default_ VirtualHost overlap on port 443, the first has precedence

Here's the vhost section of /etc/httpd/conf/httpd.conf:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName  www.domain.com
    ServerAlias www.domain.com
    #Redirect permanent / https://www.domain.com
    Redirect 302 / https://www.domain.com
</VirtualHost>

<VirtualHost *:80>
    ServerName  subdomain.domain.com
    ServerAlias subdomain.domain.com
    #Redirect permanent / https://subdomain.domain.com
    Redirect 302 / https://subdomain.domain.com
</VirtualHost>

And here's /etc/httpd/conf.d/ssl.conf:

<VirtualHost *:443>
    ServerName  www.domain.com
    ServerAlias www.domain.com
    DocumentRoot /var/www/html
    ...
    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
    SSLCertificateFile        /path/to/cert
    SSLCertificateKeyFile     /path/to/key
    SSLCertificateChainFile   /path/to/bundle
</VirtualHost>

<VirtualHost *:443>
    ServerName  subdomain.domain.com
    ServerAlias subdomain.domain.com
    DocumentRoot /var/www/vhosts/subdomain
    ...
    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
    SSLCertificateFile        /path/to/cert
    SSLCertificateKeyFile     /path/to/key
    SSLCertificateChainFile   /path/to/bundle
</VirtualHost>

And here's apachectl -S output (with my domain name redacted/substituted):

enter image description here

Any help would be greatly appreciated.

=======================================================================

Edit: I removed the word permanent, cleared my cache, and the problem persists. It seems like it has to do with the warning, right?

Also, I get a successful response regardless of which subdomain I use even when it doesn't exist. I can literally type whatever I want for subdomain and it goes to the main site. http://<anything>.domain.com and https://<anything>.domain.com both load the main site with https.

like image 214
Anthony Avatar asked Jan 22 '26 08:01

Anthony


1 Answers

The problem was simple, but elusive, at least to me. In /etc/httpd/conf.d/ssl.conf, I needed to add the following line for the VirtualHost configurations to be distinct:

NameVirtualHost *:443

Now everything works as expected.

like image 61
Anthony Avatar answered Jan 25 '26 06:01

Anthony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!