Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP shared header without using sever-side scripting?

Tags:

html

php

Simple problem I have so far always solved via PHP:

You have a site with header, menu, footer and content field.

Header, menu and footer are usually the same for each page.

  • How do you, without PHP or any other server-side language, have the header, menu and footer data exist only in one file?

So that for example you don't have ten pages (like home.html, products.html, about.html, ..) all having a copy of the static header and menu in their html files. Now if you want to change the header you have to change ten files.

I hope I made my question clear enough, if not please leave a comment :)

like image 757
cyggi Avatar asked Feb 17 '26 06:02

cyggi


1 Answers

If you're using Apache, you can use server-side includes. These basically provide include statements within HTML documents.

Install and enable mod_include. Configure

Options +Includes
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml

Use .shtml as your main page file extension. Then in your pages, you can do things like

<!--#include virtual="/header.html" -->

Nginx also supports server-side includes with it's SSI module:

location / {
    ssi on;
}

HTML:

<!--# include file="header.html" -->
like image 92
Matt S Avatar answered Feb 19 '26 19:02

Matt S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!