HTML to PHP Template
I have a folder with several hundred articles.
I would like to re-create them to PHP pages
adding a header and a footer. (PHP Template)Like:
Header
Content
Footer… from the Warrior Forum
I have a great news!
You can do this without editing any of this hundred files.
Lets create a PHP-script to do this for you.
.htaccess runs the php-script instead of html-pages
Lets suppose our script would be named as index.php
Now we have to redirect all requests on your existent pages into this script.
This can be done with the .htaccess file inside your directory.
Type this lines below into your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.html? index.php [L]
</IfModule>
Create Header and Footer of your articles
Just create header.html and footer.html files with HTML-codes of the header and footer you’d like and upload this files into the Directory where all your articles are placed.
header.html
<html><body>
<!-- BEGIN HEADER -->
<img src="..." title="My common header image">
<h1>COMMON HEADER TITLE</h1>
<!-- END HEADER -->footer.html
<!-- BEGIN FOOTER -->
<img src="..." title="My common footer image">
Copyright © 2006 MyCopy
<!-- END FOOTER -->
</body></html>The CORE of this solution — index.php
Create the index.php file with the code below:
<?php
$HEADER_FILE = "header.html";
$FOOTER_FILE = "footer.html";
$CONTENTS_FILE = basename(getenv("REQUEST_URI"));
// show the Header
readfile($HEADER_FILE);
// show the Contents
readfile($CONTENTS_FILE);
// show the Footer
readfile($FOOTER_FILE);
exit;
?>
Now upload this file into the same Directory with your articles, header.html and footer.html files.
That’s all!
Enjoy your templated Directory.







July 25th, 2006 at 04:49
Can you elaborate a bit more about precisely how to use this?
July 25th, 2006 at 05:53
Hi gann,
Lets suppose you have an articles directory with a lot of html-files at your site:
www.yoursite.com/directory
They can open and read articles like below:
www.yoursite.com/directory/article1.html
www.yoursite.com/directory/how-to-tips.html
www.yoursite.com/directory/htmltophp.html
…
In order to cover every of those articles with your header and footer follow this steps below.
1. Create a file named as
header.htmlwith your header HTML-code.2. Create a file named as
footer.htmlwith your footer HTML-code.3. Upload this 2 files into your directory folder.
4. Create
.htaccessfile in the same folder with the code above.(See the code under the ‘.htaccess runs the php-script instead of html-pages’ title)
5. Create
index.phpfile with the code above and upload it into the same folder.(See the code under the ‘The CORE of this solution — index.php’ title)
Then try to open the same articles (www.yoursite.com/directory/article1.html).
They should have your header and footer now.
I hope this helps.
July 26th, 2006 at 01:03
Thanks Michel…now it makes sense to me on how it works.
July 28th, 2006 at 02:20
Can a sidebar like a Navigation/menu be incorporated into this script?
July 28th, 2006 at 10:30
Hi gann,
Yes, sure. You can use any page-layout with your
header.htmltemplate.This example below uses a html-table layout.
<html><body><!-- BEGIN HEADER -->
<img src="..." title="My common header image">
<h1>COMMON HEADER TITLE</h1>
<!-- END HEADER -->
<table><tr><td width="100" valign="top">
<!-- BEGIN SIDEBAR -->
MENU
<li>Menu Item 1</li>
<li>Menu Item 2</li>
...
<!-- END SIDEBAR -->
</td><td>
<!-- BEGIN ARTICLE -->
July 28th, 2006 at 15:21
Thanks Michel, it works great!
July 30th, 2006 at 14:25
Michel, I have one small problem with this script. I am experimenting with and I like the concept except for one issue I am having with it.
If you go directly to a page for example “www.yoursite.com/directory/article1.html”
it works correctly. But if you go directly to the directory for example
“www.yoursite.com/directory” the template shows but in the content area I get a message like
“Warning: readfile(directory): failed to open stream: No such file or directory in /home/content/user/html/directory/index.php on line 10″
How can the script be adjusted so that when someone goes straight to the directory instead of a particular file, that the above message does not show up in the content area? Is there a line of code that I can put in the .htaccess file?
July 30th, 2006 at 14:37
Sorry…I forgot to mention that my file extensions are named “.php”. Using file extensions “.html” I get a blank page when going directly to the directory.
July 30th, 2006 at 14:45
One more thing, is it correct to add the following 3 lines of code in the .htaccess file so that different file extensions will work with the template. Since I have different file types I added the following 3 lines of code.
RewriteRule \.htm? index.php [L]
RewriteRule \.php? index.php [L]
RewriteRule \.txt? index.php[L]
Sorry for all the trouble I am putting you thru.
July 30th, 2006 at 17:48
Hi gann,
What would you like to see when you go directly to the directory?
1) You could upload yet another file (for example,
default.html) with a home page of your directory:————————————
This is the article directory
————————————
2) Then make small changes of the
index.phplike below:————————————
$HEADER_FILE = “header.html”;
$FOOTER_FILE = “footer.html”;
$DEFAULT_FILE = “default.html”;
$CONTENTS_FILE = basename(getenv(”REQUEST_URI”));
// show the Header
readfile($HEADER_FILE);
// show the Contents
if (file_exists($CONTENTS_FILE)) {
readfile($CONTENTS_FILE);
} else {
readfile($DEFAULT_FILE);
}
// show the Footer
readfile($FOOTER_FILE);
exit;
————————————
3) In order to use this solution with
.html,.php, and.txtfiles use.htaccesslike this:————————————
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(html?|php|txt) index.php [L]
————————————
I hope his helps.
July 30th, 2006 at 18:39
Michel…it worked like a charm. Thank you so much! At first it didnt work but I noticed (or I should say assumed) that you made a typo mistake in the following:
RewriteRule \\.(html?|php|txt) index.php [L]
I changed it to:
RewriteRule \.(html?|php|txt) index.php [L]
Then it worked perfectly!
Thanks Michel.
August 12th, 2006 at 11:41
[…] P.S.Michel has an excellent tip for those of us who have loads of articles on HTML format that need to converted into php pages quickly and easily. It requires setting up .htacess to run the php script. Read more here […]