David W.
David W.

Reputation: 13

.htaccess find and replace

My hosting provider has their SSL requests go through a proxy, and I want to force https through the .htaccess file, but the SSL isn't listening on 443, so doing it by port, https on, or requiring ssl in .htaccess isn't working.

I've been trying to find a way to find and replace through .htaccess. For example, .htaccess would search for "http://" and replace all occurrences of "http://" with "https://" based on the URL the person is at, ultimately forcing a secure connection. So far, I haven't found a way to do this with .htaccess, so any help would be appreciated.

I understand that I could accomplish this with JavaScript and that it's also possible with PHP, but I'm really trying to get this done through .htaccess. Thanks!

Upvotes: 1

Views: 275

Answers (2)

tc.
tc.

Reputation: 33592

In "normal" HTTP you can't. Sorry.

A normal request looks something like this:

GET / HTTP/1.1
Host: www.example.com

This looks exactly the same whether it's HTTP or HTTPS (the Host header can have a port number, but it's normally omitted for the default port).

There is an alternative syntax, which (I suspect) originates from HTTP proxy servers:

GET http://www.example.com/ HTTP/1.1

Some web servers support the second syntax (Apache does; I haven't tested others), but clients won't use the second unless they're talking to an HTTP proxy. Occasionally I consider filtering requests which use the second syntax, as they're probably an attempt to find an open proxy.

There are a couple of easy options:

  • Forward SSL to a different port.
  • Enable SSL on the webserver.

The latter is not too tricky (if you want it on a different cert, you can use StartSSL or roll your own CA), and you can choose a low-overhead cipher suite (e.g. RC4)

Upvotes: 0

Brian Hannay
Brian Hannay

Reputation: 600

RewriteEngine On 
#if it is a regular http request
RewriteCond %{HTTPS} !=on
#rewrite every page to https version
RewriteRule ^(.*)$ https://www.yoursitenamehere.com/$1 [R,L]

Upvotes: 1

Related Questions