Published by Shawn on 13 Apr 2008 at 02:37 am
HTACCESS Wrappers Tutorial
Instead of naming your PHP .php, you have the option of keeping the file extension as .html or .htm.
This only takes a few lines of code in the .htacces file, and a few more lines of code in the PHP script that runs the show.
Here is the code for the .htaccess file:
AddHandler pagex.htm
AddHandler pagex.html
Action pagex/page.php
The first two lines tell Apache to designate files of type .htm or .html to a type called ‘pagex’.
The last line instructs the web server to execute the page.php script when either of the files listed above are requested.
The path is relative, so if the script was in directory http://www.example.com/scripts/page.php, the line would read Action pagex /scripts/page.php
Now the magic…
In this tutorial we will identify the name of the file being requested, and serve the relevant content (wrapped in valid XHTML of course!).
The following is the code for page.php:
<?php
$file
= $_SERVER["PATH_INFO"];
switch(
$file){
case
“/index.html”:
include(
‘index.php’);
break;
case
“/shawngo.html”:
include(
’shawngo.php’);
break;
}
?>
$file = $_SERVER["PATH_INFO"]; returns the file name of the requested file in the form “/filename.html”. We could do some fancy string work, but that is out of scope.
The switch statement switches on $file and the corresponding .php file is included using the include statements.
Here is an example of the index.php file:
<?php
// define a few variables
$title=“Web Development is Fun!”;
$description = “PHP Scripts, Google Maps API Tutorials And More!”;
$keywords = “KML, Google Maps API, Tutorials”;
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/2001/REC-xhtml11-20010531/DTD/xhtml11-flat.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en”>
<head>
<title><?=$title?></title>
<meta http-equiv=”Content-Type” content=”text/xml; charset=iso-8859-1″ />
<meta name=”description” content=”<?=$description?>” />
<meta name=”keywords” content=”<?=$keywords?>” />
<meta name=”title” content=”<?=$title?>” />
</head>
<body>
<div id=”wrapper”>
<h1>Hello World</h1>
</div>
</body>
</html>
Wasn’t That Fun?
There are many different applications for this type of modifications. This offers the ability to insert ads into the header or footer of any or all pages of your webste. This is how some of the free hosts add advertising to the free websites they host.
My most recent application of this obfuscation is I See You Marketing where all the html pages are served up this way.