Bulk Replace
Web and code

How to Replace the Header, Menu or Footer on Every Page of a Static Site

Update the menu, header or footer across every page of a static HTML site in one pass. Regex block patterns, marker comments and safe preview steps.

Updated 5 minute read The Bulk Replace team

To replace the header, menu or footer on every page of a static HTML site, use a multi-line regex find and replace that matches the block from its opening tag to its closing tag, such as <footer\b[\s\S]*?</footer>, and replace it with the new markup across all .html files in the site folder. If your pages have marker comments around the block, match those instead, because they are more reliable. Preview the matches first: the count should equal the number of pages, one block each.

This guide covers how to inspect the blocks, the patterns for each situation, a safe run order, and how to set the site up so the next change takes one edit instead of hundreds.

Why static sites end up with this job

Hand-built or exported HTML sites usually copy the same header, navigation and footer into every page. Adding a menu item, changing a phone number or updating a legal link then means editing every file. Doing that by hand on 40 pages is tedious; on 400 it is error-prone, and you will miss some.

A bulk replace fixes the current problem. The last section of this guide shows how to avoid the next one.

Step 1: compare the blocks across pages

Open three or four pages in a text editor, including an old one and a recent one, and compare the block you want to change.

  • Identical everywhere? You can match it exactly, or by start and end tags.
  • Small differences, such as class="active" on a different menu item per page? Match by start and end tags and accept that the new block will reset those differences, or handle them in a second rule.
  • Different structure on some pages, such as an older template? Group the pages and run a separate rule for each group.

Also check the line endings and indentation in your editor's status bar. Mixed CRLF and LF files are common on sites edited on several machines.

Step 2: pick a pattern

Option A: marker comments (most reliable)

If the block is wrapped in comments, use them:

<!-- FOOTER START -->[\s\S]*?<!-- FOOTER END -->

Put the markers back in the replacement around the new content, so the next update works the same way. If your pages do not have markers yet, this is a good time to add them: make the first replacement output the new block wrapped in markers.

Option B: the element's own tags

<footer\b[^>]*>[\s\S]*?</footer>

  • \b after the tag name stops it matching similar names.
  • [^>]* allows any attributes on the opening tag, such as class or id.
  • [\s\S]*? matches everything in between, across lines, stopping at the first closing tag.

The same shape works for <header> and <nav>. It is safe as long as the block does not contain another element with the same name nested inside it.

Option C: a div with a known id or class

Older sites often use <div id="footer">. Matching to the first </div> fails if the footer contains other divs. Anchor the end on something unique that follows the block instead:

<div id="footer">[\s\S]*?</div>(\s*</body>)

Replace with the new footer followed by $1 to put the closing body tag back. Check the preview carefully with this one.

Option D: change one line, not the whole block

Often you do not need to replace the block at all. Adding a menu item can be a single-line rule:

  • Find: (<li><a href="/contact.html">Contact</a></li>)
  • Replace: <li><a href="/blog/">Blog</a></li>$1

Small, targeted rules are easier to check and less likely to disturb per-page differences.

If pages live at different folder depths, a menu written with relative links (href="about.html" on the home page, href="../about.html" one level down) is not the same block on every page. Pasting one version everywhere breaks links in subfolders. Either:

  • Use root-relative links (href="/about.html") in the new block, which work at any depth when the site is served from a domain, or
  • Run a separate rule per folder level, with the matching relative paths.

Step 4: run it safely

  1. Back up the site folder.
  2. Filter to .html and .htm (plus .php or template files if they hold the block).
  3. Run the search only. One match per page is what you want. Pages with zero matches have a different structure; pages with two may have the block twice, or the pattern is reaching too far.
  4. Inspect the longest match. If it runs past the end of the block, tighten the pattern.
  5. Apply, then open pages from different folders and check the layout and links.

In Bulk Replace, this is a multi-line regex rule. Choose the site folder with sub-folders included, filter to the HTML extensions, and check the before/after diff for each page, which shows exactly which lines each match covers. Untick any page with an unusual structure to handle it separately. Bulk Replace backs up every file first, so if the new footer turns out to have a typo, one click restores all the pages. Saving the job as a project makes the next footer change quicker still.

If something goes wrong

  • Pages lost content below the footer: the pattern was greedy or the end marker was wrong, so the match ran on. Restore from the backup and use a lazy *? or a more specific end.
  • Some pages unchanged: their block differs. Search those pages for the opening tag alone and compare the markup.
  • Broken links in subfolders: the new block uses relative links. Switch to root-relative links.
  • Strange characters in the new footer: the replacement text was inserted in a different encoding from the page. Use a tool that preserves each file's encoding, and check pages with accented text.

Step 5: stop repeating this job

ApproachHow it worksNeeds
PHP includeRename pages to .php and use <?php include 'footer.php'; ?>Host with PHP
Server-side includes<!--#include virtual="/footer.html" --> in .shtml pagesSSI enabled on the server
Static site generatorOne layout template builds every pageA build step before upload
Marker commentsKeep the copies, but make them easy to replace in bulkNothing extra

Switching to includes is itself a bulk replace: swap each copied block for the include line in one run.

For more multi-line techniques, see how to replace multiple lines of text in multiple files. If the footer change is only the year, our guide to updating the copyright year in all HTML files is quicker.

Frequently asked questions

How do I replace a block that differs slightly between pages?

Match from a fixed start to a fixed end with a lazy pattern such as <footer[\s\S]*?</footer>, so small differences in between do not matter.

Is regex safe for editing HTML?

For a known, consistent block on your own pages, yes. For arbitrary HTML with nested tags, regex can mismatch, so always check the preview.

How can I avoid this job in the future?

Use server-side includes, a PHP include or a static site generator so the header and footer live in one file.

Get three guides like this every month, plus software update notices.Subscribe →