Home arrow PHP arrow Page 4 - Managing Secure Protocol in Apache-Based Websites using PHP

Second Potential Problem: 301 Redirection from non-secure to secure protocol - PHP

When trying to maintain a secure protocol on an Apache-based website, you can expect to deal with certain issues, especially if you're also trying to rank well in the search engines. This article provides you with some solutions for two of the more difficult problems: duplicate content and 301 redirects.

TABLE OF CONTENTS:
  1. Managing Secure Protocol in Apache-Based Websites using PHP
  2. First Potential Problem: Duplicate Content!
  3. Second Duplicate Content Solution
  4. Second Potential Problem: 301 Redirection from non-secure to secure protocol
By: Codex-M
Rating: starstarstarstarstar / 5
May 28, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

There are times when you do really need to redirect the http to the https version. A good example would be to convert an http URL to the https version of the URL so that any transaction between the browser and the server that happens on that URL will be encrypted.

Another good example would be submitting sensitive data such as passwords, security numbers or even credit card information.

So how do you go about converting the non-secure protocol to a secure protocol? Using PHP you can force a specific URL to 301 redirect to its equivalent https version.

See an example below:


<?php

function url() {

$urlofthepage = 'http';

if ($_SERVER["HTTPS"] == "on") {$urlofthepage .= "s";}

$urlofthepage .= "://";

if ($_SERVER["SERVER_PORT"] != "80") {

$urlofthepage .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

} else {

$urlofthepage .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

}

return $urlofthepage;

}

$urlofthepage=url();

if ($urlofthepage=="http://www.thisistheurlthatyouneedtoredirecttohttpsversion.com/
securecontactexampleform.php")

{

// Permanent redirection

header("HTTP/1.1 301 Moved Permanently");

header("Location: https:// www.thisistheurlthatyouneedtoredirecttohttpsversion.com/securecontactexampleform.php ");

exit();

}

?>


Place this code on top of any HTML code, or before it outputs any HTML. This script must be placed on top of all PHP or HTML code. If it isn't, it won't redirect at all, and in fact will return an error.

Note that the above sample script only redirects one http version URL to its equivalent https version. You can add any number of redirections; for example, if you have a lot of secure form URLs in the domain, but are using the same template.

In this case, just add another if statement right below the firstredirection. See the example below:

<?php

function url() {

$urlofthepage = 'http';

if ($_SERVER["HTTPS"] == "on") {$urlofthepage .= "s";}

$urlofthepage .= "://";

if ($_SERVER["SERVER_PORT"] != "80") {

$urlofthepage .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

} else {

$urlofthepage .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

}

return $urlofthepage;

}

$urlofthepage=url();

if ($urlofthepage=="http://www.thisistheurlthatyouneedtoredirecttohttpsversion.com/
securecontactexampleform.php")

{

// Permanent redirection

header("HTTP/1.1 301 Moved Permanently");

header("Location: https:// www.thisistheurlthatyouneedtoredirecttohttpsversion.com/
securecontactexampleform.php ");

exit();

}

//this is the second URL to be redirected to its https version

if ($urlofthepage==
"http://www.thisisthesecondurlthatyouneedtoredirecttohttpsversion.com/
securecontactexampleform.php")

{

// Permanent redirection

header("HTTP/1.1 301 Moved Permanently");

header("Location: https:// www.thisisthesecondurlthatyouneedtoredirecttohttpsversion.com/
securecontactexampleform.php ");

exit();

}

?>




 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: