0

I am trying to set up a default HTTP to HTTPS redirect. Somehow neither mod_rewrite nor Redirect work. Am I missing something obvious?

 Listen 80
 Listen 443

<VirtualHost *:80>
         ServerName www.example.com
         ServerAlias example.com
         Redirect ^/$ https://www.example.com
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    ServerAlias example.com
    SSLEngine on
    SSLCertificateFile "/etc/ssl/certs/example.com.pem"
    SSLCertificateKeyFile "/etc/ssl/private/example.com.key"

    <Directory "/web/www.example.com">
            allow from all
            Options -Indexes
    </Directory>
    #php_admin_flag safe_mode Off
    php_admin_value open_basedir /tmp/:/web/www.example.com
    DocumentRoot /web/www.example.com
    </VirtualHost>
  • Define not working. What is the observed behaviour? – Gerald Schneider Apr 3 '17 at 11:35
  • Hi Gerald. It simply does not redirect. When I enter www.example.com, apache serves the http version of the page. – randomcoding Apr 3 '17 at 11:37
  • 3
    Your VirtualHost block doesn't contain a document root, yet the content has to come somewhere. One possibility would be that you are editing the wrong config file. Apart from that, your Redirect looks wrong. ^/$ would only match on / only, nothing else, so it wouldn't be very effective. Also, Redirect doesn't take regular expressions, you need RedirectMatch for that. Most probably you want Redirect permanent / https://www.example.com. – Gerald Schneider Apr 3 '17 at 11:40
  • @GeraldSchneider No need for a DocumentRoot directive if all you are doing is redirecting. Even if one isn't set in the global scope either, it doesn't matter because no files outside of the server configuration itself need to be accessed. – a CVn Apr 3 '17 at 11:46
  • 1
    @MichaelKjörling of course it is not needed for a redirect. But if actual content is served by the server instead of redirecting it has to be set somewhere. Hence the conclusion that there might be another config file involved. – Gerald Schneider Apr 3 '17 at 11:47
2

Here's an example from a config I wrote in the past using mod_rewrite.

<VirtualHost *:80>
  ServerName  www.foo.com
  ServerAlias foo.com   
  RewriteEngine on
  RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>


<VirtualHost *:443>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin admin@foo.com
  ServerName  www.foo.com
  ServerAlias foo.com

  SSLEngine on
  SSLProtocol all
  SSLCertificateFile /etc/apache2/ssl/www.foo.com/foo_com.crt
  SSLCertificateKeyFile /etc/apache2/ssl/www.foo.com/foo_com.key
  SSLCertificateChainFile /etc/apache2/ssl/www.foo.com/intermediate.crt

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/foo.com/public_html
</VirtualHost>
  • 1
    Not sure what good the RewriteCond is doing in the HTTP vhost, honestly. Also, you probably want a ServerName in the HTTP vhost. – a CVn Apr 3 '17 at 11:51
  • This was a few years ago, but I think the rewrite cond was to stop a recursion issue that I ran into. The fact that the vhost listens on port 80 should make this not needed but for some reason this still affected the site. This could have been an issue with a given version of apache and/or mod_rewrite at that time.This now hosts a single site and I wanted everything rewritten to use https. If the server hosts multiple sites and not all should be forced to use https you should add ServerName. – inquam Apr 3 '17 at 11:55
  • I updated the config to match what should probably fit best for this specific user and remove the fix for my strange issue. – inquam Apr 3 '17 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.