views:

32

answers:

1

I am trying to write an .htaccess rule that appends www. to the domain and s to http if required but I can't seem to find a rule or set of rules that works for each case.

The cases are...

\https://www.site.com - should just work

\http://www.site.com - should go to \https://www.site.com

\http://site.com - should go to \https://www.site.com

\https://site.com - - should go to \https://www.site.com

Any help would greatly appreciated.

+2  A: 

Try this rule:

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://www.%2%{REQUEST_URI} [L,R=301]

This should cover both cases in which either the request was not HTTPS (first condition) or the host does not start with www. (second condition). In that case the third condition will grab the host without the starting www. (if present) that is then used in the rule’s substitution.

Gumbo
No joy, I was getting an internal server error when I run this.
Toby
@Toby: What does the error log say? Did you use obligatory `RewriteEngine on`?
Gumbo
OK, I was doing something daft to get that error, my bad. However it doesn't cover _https_://site.com to _https_://www.site.com
Toby
Gumbo
I have read in some places about an issue when sites use https:// that is caused by the redirect being blocked because the server automatically doesn't trust the connection, this could well be the case here. Thanks for your help.
Toby
@Toby: Maybe HTTPS requests are handled by another virtual host or in another directory. You could try the opposite, redirect every HTTPS request to HTTP by replacing `!on` with `on` and `https://` with `http://`.
Gumbo