RewriteCond not working on force https for HTTP_HOST
11,643
Solution 1
You need to move your bottom http->https
rule to the top of your .htaccess just below RewriteBase /
line. Also use this slightly modified rule:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !local [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L,NE]
Solution 2
You may use
RewriteCond %{ENV:HTTPS} off
some time https flag not working . Don't know why. But I have found a solution to use ENV:HTTPS . This stop redirect loop for me.
Related videos on Youtube
Author by
n1ch0la5
Updated on June 20, 2022Comments
-
n1ch0la5 almost 2 years
I am trying to write a condition that will force https on the live host name(domain.com), but not if it's on our local testing host name (domain.local).
Here is what I have:
#force https RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} !domain\.local [NC] RewriteRule ^(.*?)$ https://www.domain.com/$1 [L,R=301]
This condition rewrites to https but also redirects domain.local to domain.com
I've also tried this condition in place of the middle condition which doesn't do anything on .local or .com:
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
Here is the complete contents of my htaccess file
Options +FollowSymLinks RewriteEngine On RewriteBase / #force ssl RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^domain\.com RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L,NE] #rewrite payments subdomain RewriteCond %{HTTP_HOST} ^payments\.domain\.local$ [NC] RewriteRule ^(.*)$ https://otherdomain.com/dmi_domain.htm [NC,R=301,L] RewriteCond %{HTTP_HOST} ^payments\.domain\.com$ [NC] RewriteRule ^(.*)$ https://otherdomain.com/loanadmin/dmi_domain.htm [NC,R=301,L] #agents folder to subdomain RewriteCond %{HTTP_HOST} ^agents\.domain\.local$ [NC] RewriteRule ^agents/(.*)$ /agents/$1 [L,P] RewriteCond %{HTTP_HOST} ^agents\.domain\.com$ [NC] RewriteRule ^agents/(.*)$ /agents/$1 [L,P] #force www for .com RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTP_HOST} !^payments\. [NC] RewriteCond %{HTTP_HOST} !^agents\. [NC] RewriteCond %{HTTP_HOST} !^domain\.local RewriteCond %{REQUEST_URI} !^/blog/ RewriteCond %{REQUEST_URI} !^/questions/ RewriteRule ^(.*?)$ https://www.%{HTTP_HOST}/$1 [L,R=301] #force www for .local RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTP_HOST} !^payments\. [NC] RewriteCond %{HTTP_HOST} !^agents\. [NC] RewriteCond %{HTTP_HOST} !^domain\.com RewriteCond %{REQUEST_URI} !^/blog/ RewriteCond %{REQUEST_URI} !^/questions/ RewriteRule ^(.*?)$ http://www.%{HTTP_HOST}/$1 [L,R=301] #force trailing slash RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.*)/$ RewriteCond %{REQUEST_URI} !^/rate-panel/xml/ RewriteCond %{REQUEST_URI} !^/blog/ RewriteCond %{REQUEST_URI} !^/questions/ RewriteRule ^(.*)$ $1/ [L,R=301] #get rid of index.php in url RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/blog/ RewriteCond %{REQUEST_URI} !^/questions/ RewriteRule ^(.*)$ /index.php/$1 [L]
thanks
-
n1ch0la5 almost 10 yearsThanks, but still doesn't work. Nothing happens on either .com or .local.
-
anubhava almost 10 yearsCan you update your question with out latest .htaccess? Is this some kind of PHP/CMS framework?
-
n1ch0la5 almost 10 yearsHtaccess updated. The site is on the codeigniter php framework. thanks!
-
anubhava almost 10 yearsok rule looks fine. Is it possible that CI is forcing it back to
http://
? Can you run your request in Firebug and see what redirections you get in Net tab. -
n1ch0la5 almost 10 yearsI meant to add that if i remove the condition the redirect works fine, but it redirects both .local and .com to https.I don't see any redirects in the Net tab, but I don't know exactly where to find the redirects.
-
anubhava over 6 years@giovannipds
%{HTTPS}
is standard Apache variable. You may be behind proxy or firewall. Check server documentation from your hosting providers. -
giovannipds over 6 yearsYes, but I've figured out right now that the problem is with my hosting server (I've compared the same codes on two different servers). Anyway, I'm still looking for an alternative. Best regards.
-
Ariful Islam about 6 yearsIt's not for all time work. anyway, did you found your proper solution?
-
giovannipds about 6 yearsI'm not sure, but probably. Sorry if I haven't posted anything about it, I was focused. Thank you anyway for your attention.
-
Ariful Islam about 6 yearsDo you have still problem?
-
giovannipds about 6 yearsNo, not anymore. Thanks mate.
-
james6848 over 5 yearsIn my case I needed to use RewriteCond %{ENV:HTTPS} !=on ('off' did not work in my particular environment).
-
james6848 over 5 yearsIn my case I needed to use RewriteCond %{ENV:HTTPS} !=on. So the environmental variable mentioned below and specifically '!=on' instead of 'off'.