freaki1 freaki1 - 5 months ago 24
CSS Question

Export parts of a web page to another file

So i have made a website with more than 10 pages, but now I want to export the head, the header and the footer to another file, because if I want to change something on the header/footer I don't want to edit all 10 pages. Is it possible to make a header.html and make the other html file use the code from that file or somehting?


What you're describing is an "include" and here's how to do it with PHP. I recommend using a server side language like PHP to do the include because it will definitely be indexed by Google and you don't need to worry that the user has javascript disabled or poorly supported.

First of all, you won't be able to do this unless your webpages are either saved as PHP (with .php extension) or your HTML pages (.html extension) are being parsed by PHP. Obviously you'll also need PHP running on your server.

Take the chunks you want to "include" and save them into their own files (header.php, footer.php, etc.). Put them in their own folder under your assets area (such as a directory called includes or inc). Then, in your web pages, use PHP to include those files:

<?php include_once($_SERVER['DOCUMENT_ROOT'].'/include/header.php') ?>

I like to use the $_SERVER['DOCUMENT_ROOT'] variable so I don't need to remember the relative path to the include directory. Use that include line exactly where you would place the original header html.

If you want to keep your files as HTML but have them parsed as PHP, you'll need to make sure your server is parsing HTML files as PHP. You can direct the server to do this in the .htaccess (assuming Apache) where you'll need a line like this:

AddHandler application/x-httpd-php .php .htm .html

This line will be different for different host environments, so you may need to tweak that.