Sign in to PrePAN


User's Modules

Apache::Inject Apache directive for injecting HTML headers and footers

Apache::Inject is a mod_perl module that adds an Apache directive called Inject.

The Inject directive takes one or two arguments, which correspond to the file names of two HTML files in the document root. The contents of these files are then inserted into any requested HTML file. The first file (the header) is inserted at the top of the body of the requested HTML file, while the second, optional file (the footer) is inserted at the bottom of the body.

The directive is smart enough to place the header and footer in the proper places. The contents of the header file is inserted after any elements belonging to <head> and before any elements belonging to <body> (regardless of whether any explicit <head> or <body> tag is present in the source of the requested HTML page). Likewise, the contents of the second file is placed before any potential final </html>.

The Inject directive serves a much more specific purpose than server-side includes. It is designed for injecting headers and footers that belong in the <body> element, such as headings, menu bars and copyright notices. While you can technically include <head> elements in your header file, Apache::Inject will place them in the body of the HTML page and not in the head.

The main benefit over server-side includes is that the header and footer is specified in the server configuration instead of the HTML files themselves. Thus, it is useful for adding headers and footers to a large number of pre-existing static HTML pages. Furthermore, this means that the headers and footers on all pages can be changed at once by a single change in the server configuration.

Please note:

  • The Inject directive is valid only inside directory sections, such as <Directory>, <Location> and <FilesMatch> blocks. It is valid in .htaccess files if AllowOverride Limit/AuthConfig/All is enabled.

  • The file paths given to Inject are relative to the document root of the current server or virtual server -- not the directory to which the current directory section or .htaccess file applies. They should be specified without a leading slash.

jocap@github 4 comments