It would be usefull to also have a checkbox where one could enable http, https or both.

Comments

jibus’s picture

Redirect all http trafic to https you mean ?

neograph734’s picture

Yes, drupal has a check in htaccess to see if the user came from https and then redirects to www. Or non www. with the given protocol. But there is no option to force always https. Or always http.

Having a checkbox that adds https rewrite to the htaccess would be an addition to this module.

jibus’s picture

So, a short form with 3 radios :
- Allow trafic from both http and https (default)
- Force all trafic through http protocal only
- Force all trafic trough https protocol only

(label won't be the same, but its just to have an idea)

What can possibility go wrong ? If https is not configured correctly, the all site could crash. We need to detect if https is on.

Anything else ?

neograph734’s picture

The only extra thing I can think of is to limit the number of redirects to a minimum for speed and SEO reasons.

So it would be wrong to do something like this:

  1. Redirect all traffic to http / https
  2. Redirect all traffic to www / non www

Better would be to do all in one, so you might need to combine this with the part that forces www.

  1. Redirect all traffic to http / https and www / non www
jibus’s picture

I think we can manage to avoid multiple redirection.

Depending of the choice, we adapt the rewrite rules, imo.

On one of my site , i have this before the drupal redirect rules

 RewriteEngine on
 RewriteCond %{HTTPS} off [OR]
 RewriteCond %{HTTP_HOST} ^website\.com*
 RewriteRule ^(.*)$ https://www.website.com/$1 [L,R=301]

I am not an expert in Apache rules, but maybe we can rewrite these parts depending of the option choosen by the user

 # To redirect all users to access the site WITH the 'www.' prefix,
 # (http://example.com/... will be redirected to http://www.example.com/...)
 # uncomment the following:
 RewriteCond %{HTTP_HOST} !^www\. [NC]
 RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment the following:
  # RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  # RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
neograph734’s picture

That seems quite easy. If https is required an extra 's' could be added in the rewrite rule after the fixed 'http'.

 # To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# uncomment the following:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http[s]://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
-------------------^

Regular Drupal .htaccess

Lines 59-64

# 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]

Lines 89-94

# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# uncomment the following:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Mimicing this will give the desired effect.

Proposion

Add 3 variables for the 3 oprions in #3.

Default

RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]

http

RewriteRule ^ - [E=protossl]

https

RewriteRule ^ - [E=protossl:s]

Both redirects

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

So the options on the form would control the ouput of the protossl environmental variable. Would something like that work?

jibus’s picture

It requires some tests but yes.

I will write a patch based on your contribution.

neograph734’s picture

Awsome, looking forward to test this :)

neograph734’s picture

Hi Jibus, do you perhaps have any news about the patch?

jibus’s picture

Will work on this, this week

jibus’s picture

Status: Active » Needs review
StatusFileSize
new4.88 KB

Sorry for taking so long...

This patch add two options :
- HTTPS_mixed_mode (default) : Enable mixed-mode HTTP/HTTPS (allow trafic from both HTTP and HTTPS. This is the standart drupal behavior
- HTTPS_force_redirect : Enable HTTPS and redirect all HTTP trafic (force all trafic through HTTPS protocol only)

neograph734’s picture

Thanks for getting back to me :)

I've discovered some issues:
- There is a semilicon in htaccess.admin.inc line 448 (patch line 29) that breaks the htaccess admin ui.

$ssl_config = (variable_get('htaccess_settings_ssl', 'HTTPS_mixed_mode'); == 'HTTPS_mixed_mode' ? "%{ENV:protossl}" : "s");
                                                                        ^

- There appear to be semilocons missing at ends of lines 458, 465 (patch 42 & 52) also breaking the admin ui. (This is actually a new issue as it already was like this on dev). Adding them made the page work again.

$with_www_config .= "#RewriteRule ^ http". $ssl_config ."://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]" . PHP_EOL
                                                                                                                 ^

$with_www_config .= "RewriteRule ^ http". $ssl_config ."://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]" . PHP_EOL
                                                                                                                ^

- The patch replaces content in htaccess, but since everyting is handled in the www redirect section, this gets never replaced; resulting in an invalid htaccess file. This part can be removed from the patch:

-  RewriteRule ^ - [E=protossl]
-  RewriteCond %{HTTPS} on
-  RewriteRule ^ - [E=protossl:s]
+  %%%ssl_config%%%

- Following the pattern, it either switches to https if www is to be added or removed. In the case we force users to www, the htaccess files checks if there is www in the url and performs the replacement. This means http://example.com, which is missing www, will be rewritten to https://www.example.com, but http://www.example.com won't.

I solved this in the past by adding the following snippet to the custom settings field, but that does result in a second redirect:

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

I'll see what happend if we'd add this snippet just above the redirects and replace the last [L] with a chain [C] so the next set of rules will be processed as well. But I am no htaccess expert unfortunately.

Thanks for the patch and I'll let you know what I find!

jibus’s picture

Thanks for your comment. I didn't test enough... I committed the changes and fixed the error you mentionned.

There's the last and important concern about the rewritting of the domain if https redirect is enable. I think in that case, we have to add some custom code :

In htaccess, after the code:
RewriteEngine on

add

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]   

shoud do the work

neograph734’s picture

I've been using the last code block of my last comment to do just this. This is a little more flexible then simply rewriting to domain.com. Today I've been trying to find out what the x-forwarded-proto line was for (I found the snippet some time ago) but I am not sure. I think to remember it was required for some configururations and therefore I left it in.

jibus’s picture

Your last block has been added when the user select the option : Enable HTTPS and redirect all HTTP trafic (force all trafic through HTTPS protocol only).

neograph734’s picture

I'll have a look soon. Thanks!

neograph734’s picture

This seems to be working nicely, will report in any errors occur in the following days.

neograph734’s picture

Status: Needs review » Reviewed & tested by the community

I believe this is working nicely. The htaccess file is looking and working as expected. I'd say this is RTBC.

jibus’s picture

Status: Reviewed & tested by the community » Closed (fixed)

Thanks !

neograph734’s picture

Just a heads up, but you might want to add https support to the module description page. I believe it will convince more people to use the module.

And thank you!

jibus’s picture

Added, thanks !