.htaccess 301 redirects + https to http

Events happening in the community are now at Drupal community events on www.drupal.org.
crutch's picture

Searched google and tried many methods with D7 .htaccess 301 redirects and they continue not work. Can anyone point to a method that absolutely works for D7?

I also need to discover the best way to forward https to http.

Comments

?

sixstreet's picture

are you trying to push the whole site to https or just select urls?
bkc

the whole site

crutch's picture

the whole site

this will push the whole site

sixstreet's picture

this will push the whole site to use https in our env. add to docroot .htaccess

Redirect HTTP to HTTPS

RewriteCond %{HTTP_HOST} ^yourdomain.com$ [NC]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

cool thanks. Does the line

crutch's picture

cool thanks. Does the line position in .htaccess matter. ie should it be first?

it can matter depending what

sixstreet's picture

it can matter depending what else your .htaccess is doing. I would put it at the end and see if it performs as you expect. in our d7 env, it's placed just before the "" section near the end of the file.

ah I see, I need to do the

crutch's picture

ah I see, I need to do the reverse, https should always go to http

Before I mess something up

fchandler's picture

The .htaccess that ships w/ D7 has these lines, starting at line 56.

# Set "protossl" to "s" if we were accessed via https://. This is used later
# if you enable "www." stripping or enforcement, in order to ensure that
# you don't bounce between http and https.
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]

Further down, where one would enforce the www or not, at line 92, I un-commented this;

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

My question is, would I comment these out and add your lines to force https with the www?

I would edit your lines to this in order to force https and www?

RewriteCond %{HTTP_HOST} ^yourdomain.com$ [NC]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

And this will always bring up the https and www no matter what the user types in (that is close to the real address)?

yes no yes

fchandler's picture

To answer my questions.
Would I comment these out? Yes - comment or delete
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]

No - on my site the rule that worked is
RewriteCond %{HTTP_HOST} ^onemoon.net$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This line caused errors in testing - variable not supported:
RewriteCond %{HTTP:X-Forwarded-Proto} !https

Yes - Adding these lines always forces https and www no matter what I type in the address bar. Your mileage may vary.

questions

fchandler's picture

could someone answer my questions?

It's all black magic to me

Screenack's picture

Frank,

I've used this site in the past. Not flawless, but helpful for debugging rewrite rules:
http://martinmelin.se/rewrite-rule-tester/

Thanks Kyle

fchandler's picture

I tried the site and think I am getting mixed results. So I ran the site with the rules as I have them:
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

got these results:

Match? Rule Resulting URL

1 yes RewriteRule ^ - [E=protossl] -
2 no RewriteCond %{HTTPS} on -
3 skip RewriteRule ^ - [E=protossl:s] -
4 yes RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] -
5 no RewriteCond %{HTTP_HOST} . -
6 skip RewriteCond %{HTTP_HOST} !^www\\. [NC] -
7 skip RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] -

Then I ran with revised rules:
RewriteCond %{HTTP_HOST} ^onemoon.net$ [NC]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

and got this:

Match? Rule Resulting URL

1 no RewriteCond %{HTTP_HOST} ^onemoon.net$ [NC] https://www.onemoon.net
2 skip RewriteCond %{HTTPS} off https://www.onemoon.net
3 skip RewriteCond %{HTTP:X-Forwarded-Proto} !https https://www.onemoon.net
4 skip RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] https://www.onemoon.net

Although the second produced the desired url, it looks like the site is skipping a number of rules. So the questions and concerns are still open (I think).

another test site

fchandler's picture

So I found this site http://htaccess.madewithlove.be/ and ran the test. I used this for the url to test - http://onemoon.net

This is what I input for the rule:
RewriteCond %{HTTP_HOST} ^onemoon.net$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This was the result:
output url
https://www.onemoon.net/
debugging info
1 RewriteCond %{HTTP_HOST} ^onemoon.net$ [NC] This condition was met
2 RewriteCond %{HTTPS} off This condition was met
3 RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] This rule was met, the new url is https://www.onemoon.net/
The tests are stopped, using a different host will cause a redirect

Other rules kicked up errors. I am not sure if the rules were truly ineffectual or if the test site had issues with testing.

thanks will try this one

crutch's picture

thanks will try this one

triDUG

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: