#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    232
    Rep Power
    16

    Question Help w/ .htaccess HTTPS Redirect?


    I have an existing .htaccess file with redirects for forcing "www" and removing index.php from the URLs (it's an Expression Engine site). There's also a redirect for the IP to the URL. Now, I need to force the site to use HTTPS. No matter where I place the redirect, when I try to access the site, I get a redirect/loop error. Any help would be appreciated. Here's what I currently have:
    Code:
    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
    
      # Force WWW
      RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
      RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
    
      # Removes index.php from ExpressionEngine URLs
      RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
      RewriteCond %{REQUEST_URI} !/cp/.* [NC]
      RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    
      # Directs all EE web requests through the site index file
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /index.php?/$1 [L]
      
      # Rewrite and direct IP address to URL
      RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XX
      RewriteRule (.*) http://www.domain.org/$1 [R=301,L]
    </IfModule>
    And I need to place either this:
    Code:
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
    Or this:
    Code:
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    Thank you for your help!
  2. #2
  3. Headless Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,966
    Rep Power
    9647
    The best way to detect HTTPS is with %{HTTPS}, as the port alone isn't technically enough. (It is in practice, but really seeing %{HTTPS} is nicer than seeing port numbers.) Besides adding in that rule, all you should need to do is change all your http:// to https://. If you're getting redirects then make sure EE isn't the one doing it.

    Other things I'm changing:
    1. You can consolidate the two hostname checks by simply looking for whether it is not www.domain.org; uses = for a literal string match.
    2. You can put the HTTPS check in there too, since the end result for both is to redirect.
    3. When doing redirects based only on conditions (ie, all your logic is in RewriteConds and nothing in the RewriteRule) then you don't need to match (.*) - all you need is a ^ to match anything at all, then %{REQUEST_URI} instead of $1. It's a bit more efficient. The REQUEST_URI will also contain the query string, which $1 would not.
    4. Domains are always lowercase. Even if they weren't, you should avoid case-insensitive URLs because search engines will identify them as duplicate content.
    Code:
    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
    
      # Force HTTPS and WWW
      RewriteCond %{HTTPS} off [OR]
      RewriteCond %{HTTP_HOST} !=www.domain.org
      RewriteRule ^ https://www.domain.org%{REQUEST_URI} [R=301,L]
    
      # Removes index.php from ExpressionEngine URLs
      RewriteCond %{THE_REQUEST} ^GET.*index\.php
      RewriteCond %{REQUEST_URI} !/cp/.*
      RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    
      # Directs all EE web requests through the site index file
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /index.php?/$1 [L]
    </IfModule>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    232
    Rep Power
    16
    Thanks for taking the time to look at this and walk me through the edits. I was very close. Unfortunately, this still doesn't work. I get a "Could not open the page, too many redirects" error. Any ideas?
  6. #4
  7. Headless Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,966
    Rep Power
    9647
    What URL are you trying and where is it redirecting to?

IMN logo majestic logo threadwatch logo seochat tools logo