I've got just one page that I want to force to be accessed as an HTTPS page (PHP on Apache). How do I do this without making the whole directory require HTTPS? Or, if you submit a form to an HTTPS page from an HTTP page, does it send it by HTTPS instead of HTTP?
Here is my example:
http://www.example.com/some-page.php
I want it to only be accessed through:
https://www.example.com/some-page.php
Sure, I can put all of the links to this page pointed at the HTTPS version, but that doesn't stop some fool from accessing it through HTTP on purpose...
One thing I thought was putting a redirect in the header of the PHP file to check to be sure that they are accessing the HTTPS version:
if($_SERVER["SCRIPT_URI"] == "http://www.example.com/some-page.php"){
header('Location: https://www.example.com/some-page.php');
}
But that can't be the right way, can it?
BTW, please pay no attention to the URL. I know that if it were actually a page where there was a shopping cart, etc., you would do it a different way. Think of it as a page from a site that sells one item for one price where you type in your credit card info to be submitted to a payment gateway on an external site for the express purpose of charging your card one time.
I just created a .htaccess file and added :
Simple !
For those using IIS adding this line in the web.config will help:
A full example file
That easy.
The PHP way:
The Apache mod_rewrite way:
You should force the client to request HTTPS always with HTTP Strict Transport Security (HSTS) headers:
Please note that HSTS is supported in most modern browsers, but not universal. Thus the logic above manually redirects the user regardless of support if they end up on HTTP, and then sets the HSTS header so that further client requests should be redirected by the browser if possible.
You shouldn't for security reasons. Especially if cookies are in play here. It leaves you wide open to cookie-based replay attacks.
Either way, you should use Apache control rules to tune it.
Then you can test for HTTPS being enabled and redirect as-needed where needed.
You should redirect to the pay page only using a FORM POST (no get), and accesses to the page without a POST should be directed back to the other pages. (This will catch the people just hot-jumping.)
http://joseph.randomnetworks.com/archives/2004/07/22/redirect-to-ssl-using-apaches-htaccess/
Is a good place to start, apologies for not providing more. But you really should shove everything through SSL.
It's over-protective, but at least you have less worries.