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)


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]

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.


<img src="..." title="My common header image">
<!-- END HEADER -->


<img src="..." title="My common footer image">
Copyright © 2006 MyCopy
<!-- END FOOTER -->

The CORE of this solution — index.php

Create the index.php file with the code below:

  $HEADER_FILE = "header.html";
  $FOOTER_FILE = "footer.html";
  $CONTENTS_FILE = basename(getenv("REQUEST_URI"));

  // show the Header

  // show the Contents

  // show the Footer

Now upload this file into the same Directory with your articles, header.html and footer.html files.

That’s all!
Enjoy your templated Directory.


by Michel Komarov, © Copyright 2006.


Share in social bookmarking:These icons link to social bookmarking sites where readers can share and discover new web pages.  digg BlinkList Reddit NewsVine YahooMyWeb co.mments

12 Responses to “HTML to PHP Template”

  1. gann_144 Says:

    Can you elaborate a bit more about precisely how to use this?

  2. michel Says:

    Hi gann,

    Lets suppose you have an articles directory with a lot of html-files at your site:

    They can open and read articles like below:

    In order to cover every of those articles with your header and footer follow this steps below.

    1. Create a file named as header.html with your header HTML-code.
    2. Create a file named as footer.html with your footer HTML-code.
    3. Upload this 2 files into your directory folder.
    4. Create .htaccess file 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.php file 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 (
    They should have your header and footer now.

    I hope this helps.

  3. gann_144 Says:

    Thanks Michel…now it makes sense to me on how it works. :-)

  4. gann_144 Says:

    Can a sidebar like a Navigation/menu be incorporated into this script?

  5. michel Says:

    Hi gann,

    Yes, sure. You can use any page-layout with your header.html template.
    This example below uses a html-table layout.

    <!-- BEGIN HEADER -->
    <img src="..." title="My common header image">
    <!-- END HEADER -->
    <table><tr><td width="100" valign="top">
    <!-- BEGIN SIDEBAR -->
    <li>Menu Item 1</li>
    <li>Menu Item 2</li>
    <!-- END SIDEBAR -->
    <!-- BEGIN ARTICLE -->
  6. gann_144 Says:

    Thanks Michel, it works great!

  7. gann_144 Says:

    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 “”
    it works correctly. But if you go directly to the directory for example
    “” 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?

  8. gann_144 Says:

    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.

  9. gann_144 Says:

    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.

  10. michel Says:

    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.php like below:
    $HEADER_FILE = “header.html”;
    $FOOTER_FILE = “footer.html”;
    $DEFAULT_FILE = “default.html”;
    $CONTENTS_FILE = basename(getenv(”REQUEST_URI”));

    // show the Header

    // show the Contents
    if (file_exists($CONTENTS_FILE)) {
    } else {

    // show the Footer

    3) In order to use this solution with .html, .php, and .txt files use .htaccess like this:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule \.(html?|php|txt) index.php [L]

    I hope his helps.

  11. gann_144 Says:

    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.

  12. Nancy P Redford’s Practical Marketing Tips » Sone Of The Best Scripts In Life Are FREE! Says:

    […] 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 […]

Got a question?   Leave a Reply

You must be logged in to post a comment.