3

I'm trying to use .htaccess to send all traffic to https. I'm using the example in this answer: Need to redirect all traffic to https

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

This isn't redirecting anything for me. I placed it in var/www but it doesn't seem to have any effect. What did I do wrong?

Community
  • 1
  • 1
Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
  • Check that you have `mod_rewrite` enabled. – vee Aug 15 '13 at 05:15
  • Put some garbage test at the top of .htaccess and see if you get 500 or not. – anubhava Aug 15 '13 at 10:01
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Nov 05 '16 at 15:19

2 Answers2

4

Try [R,L] at the end of the rewrite rule:

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

R redirects to the provided link, and L breaks the flow if condition is met. For reference you can see:

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Nasir
  • 41
  • 4
3

If that's the only rules you have in your htaccess file and it's in your document root then you need to check a few things because the rules are fine.

  1. Make sure mod_rewrite is loaded. There should be a line in your httpd.conf file that looks something like:

    LoadModule rewrite_module modules/mod_rewrite.so
    

    Make sure it's uncommented.

  2. Make sure the directory that your htaccess file is in (should be your document root) is allowed to override server settings via htaccess. In your vhost or server config, there should be something along the lines of

    <Directory "/var/www/">
        AllowOverride All
    
        ... (some other stuff)
    </Directory>
    

    Make sure the AllowOverride is at least FileInfo

  3. Make sure your document root is actually where your htaccess file is in. Your vhost config should have a line like:

    DocumentRoot /var/www/
    
  4. Make sure the document root is for the right vhost. If you have separate vhosts for SSL and non-SSL, make sure the htaccess file is in the document root for the non-SSL vhost.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220