Redirect from http://sub.site.com to https://sub.site.com is instead going to http://www.sub.site.com

March 31, 2017 238 views
Apache Ubuntu 16.04

I am using LetsEncrypt to add SSL certs to all my domains. I used the "force redirect to https" option when I set up the cert, which added the redirect to my *:80 virtualhost and created a *:443 virtualhost with the correct config. However, it's behaving really strangely.

If you go to http://blog.ryangiglio.com, it 301 redirects you to http://www.blog.ryangiglio.com. Nowhere in my config do I see a reason for that to happen, and I can't figure out how to view the level of logs that would help me debug the issue.

Here are my conf files:

blog.ryangiglio.com.conf

<VirtualHost *:80>
    ServerName blog.ryangiglio.com

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =blog.ryangiglio.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

blog.ryangiglio.com-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName blog.ryangiglio.com

    DocumentRoot /var/www/blog.ryangiglio.com/public_html
    ErrorLog /var/www/blog.ryangiglio.com/error.log
    CustomLog /var/www/blog.ryangiglio.com/access.log combined

  <Directory /var/www/blog.ryangiglio.com/public_html/>
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>
SSLCertificateFile /etc/letsencrypt/live/blog.ryangiglio.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/blog.ryangiglio.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
</IfModule>

Any help would be appreciated. Thanks!

2 Answers

Hi @ryangiglio
Looking at those two configurations, should never result in 301 to www.sub.site.com
Are you sure there might not be hiding a configuration somewhere else? Or maybe your .htaccess has some redirects?
Try running this to list all the VirtualHosts: sudo apache2ctl -S

  • Agreed - there's nothing here that should be doing this which is why it's so strange!

    The blog.ryangiglio.com site doesn't have an .htaccess file at all

    When I run sudo apache2ctl -S I see all the virtualhosts on the server, but I'm not sure what else I can learn from that.

    Thanks!

    • @ryangiglio
      The sudo apache2ctl -S lists all the configuration files. Have a look in each of them to see if you might have made a tiny mis-configuration.
      Otherwise, you're more than welcome to post all your configurations and we ( @jtittle and me ) will do our best to track down any issues.

@ryangiglio

I'm seeing something entirely different on my end, though neither redirect to anything when I access them.

When I visit https://blog.ryangiglio.com/, I see one site with two posts. It doesn't redirect to www.

When I visit http://www.blog.ryangiglio.com/, I see an entirely different website.

...

When checking the IP they resolve to, they both resolve to the same IP address, so the issue here is configuration. Are there any other configuration files that you can provide? Something is separating the two and pointing to two entirely different directories from what I'm seeing on my end.

  • Sorry if I wasn't clear on what I'm looking for/what the problem is

    What I want: When you visit http://blog.ryangiglio.com it to redirect to https://blog.ryangiglio.com and display the blog you see with 2 posts

    What it's doing instead: Redirecting to http://www.blog.ryangiglio.com and showing the "default" document root, which is an entirely different website that's also hosted on this server under a different VirtualHost.

    What other config files would be helpful to include?

    Thanks!

    • @ryangiglio

      We'd need to see all configuration files that handle that sub-domain as they one that you've provided wouldn't cause two different sites to show up.

      If you take a look at the below, you'll see what I mean by two different sites showing up and neither redirect.

      https://imgur.com/a/wq3IX

      From the looks of the above, content is being pulled from two different directories.

      1). First screenshot is of https://blog.ryangiglio.com
      2). Second screenshot is of http://www.blog.ryangiglio.com

Have another answer? Share your knowledge.