Directing HTTP requests to HTTPS if initial connection is HTTPS but not if it is HTTP

I have a site running WordPress on Apache server and I am attempting to provide both HTTP and HTTPS connections via the same site. I want to allow connections over HTTP without forcing a redirect to HTTPS, unless the client is connecting initially via HTTPS then I want all subsequent HTTP requests to be forwarded to HTTPS to avoid issues with CORS and unsecured content warnings.

I am having some trouble turning up results on how to effectively do this with mod_rewrite alone. Most solutions I find try to force the connections to redirect to HTTPS regardless and will not allow an HTTP connection or vice versa. I have tried a few mod rewrite conditions including making use of the referer string but none seem to work thus far. I must be missing something because I feel that this is indeed possible but I and my search engines alone are stumped.

Read More

Maybe I’m just doing something wrong or is this kind of functionality beyond Mod_Rewrite?
I was thinking to use a PHP script but was worried it wouldn’t work for some static files since WordPress doesn’t handle those requests.

Update:

I have made a php script to detect the version. It sets a cookie which expires in 20 seconds from being set, this is read by Mod_Rewrite and if set it redirects the URLs to HTTPS. This works for most of the subsequent requests of an initial HTTPS request. A few URLs seem to be unaffected by it, not sure exactly why as the cookie hasn’t expired by the time of these file requests and the particular rules are before the static file bypass rules in the htaccess file. At any rate that was easy enough to fix by setting the file urls to protocol-less versions.
Some third party sites need domains rewritten though, as they serve https from other domains. On that note I don’t think this is actually possible without buffering the whole page and actually re-writing the URLs.

Related posts

Leave a Reply

2 comments

  1. It is possible to detect the initial connection but this must be done using Server Side code, like a PHP script. Then using the detection can be done at Mod_Rewrite level.

    Add in the WordPress constraint and things get complicated.

    WordPress isn’t built to facilitate one install with both protocols allowing access to content. So to accomplish this would require a custom plugin using the detection mentioned earlier, and instead of using Mod_Rewrite to direct requests on the server, we have to buffer WordPress output and logically replace/rewrite URLs in the page before they go to the user if and only if the initial connection for the page is in SSL.

    There is only one plugin I have found which does something similar to this, however it doesn’t do dynamic detection only gives admin/editors a checkbox option to make a page SSL secured. The plugin is called WordPress HTTPS

    Dynamic detection and redirection isn’t something SSL was meant for anyways, it’s either on or off, and most pages need it that way.
    I was originally trying to provide both so I could use a self-signed certificate without worrying that users would get the “warning unsecured connection” messages from their browsers by forcing them to use only SSL connections.

    So I’ll be purchasing a cert or making a custom plugin.

  2. tkausl is right, you don’t really need to do mod_rewrite. You should be able to format links without the protocol and it will automagically select for you.

    You can see that google does this with their hosted libraries:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    *Note the lack of http: or https: this will follow the protocol requested by the user.