Content rewriting
Content rewriting: rewrite the HTML, CSS and JavaScript returned to visitors on the node. Link rewriting, text replacement and forced HTTPS, with a rule tester.
What it does
Before the node returns a page to the visitor, it rewrites the HTML, CSS and JavaScript content by your rules. Nothing changes on your origin.
When to use it
- The site moved to a new domain but pages still link to the old one: change them all to the new domain.
- Change hard-coded http links in pages to https to avoid mixed content on HTTPS pages.
- Replace a piece of text or a script address without changing origin code.
Where to configure it
In the console: Sites, Content rewriting. Pick a site, then manage its rules. Changes take effect after publishing. The "Rule tester" at the bottom lets you try rules before publishing.
Settings
At the top set the "Per-response rewrite limit": default 2 MiB, adjustable from 64 KiB to 16 MiB; the rest of a larger response is sent unchanged. Each site can have 50 rules, which can be enabled, disabled and reordered.
Common fields
| Setting | Description |
|---|---|
| Name | Up to 64 characters |
| Type | Link rewrite, text replacement, force HTTPS |
| Paths | Where the rule applies, up to 10. A prefix such as /blog/; with * it is a wildcard and the whole path must match, such as /*.html; empty means the whole site |
| Content types | HTML, CSS, JavaScript, multiple choice |
| Enabled | Switch |
Link rewrite
| Setting | Description |
|---|---|
| Old hosts | Up to 20, without port, such as old.example.com |
| New host | May include a port, such as www.example.com:8443 |
| Scheme | Keep, change to https, change to http |
In HTML only links in the href, src, srcset and action attributes are changed; in CSS and JavaScript only explicit http:// and https:// links. Links with a port are left alone.
Text replacement
| Setting | Description |
|---|---|
| Find | Text to find, up to 256 bytes |
| Replace with | Up to 1024 bytes |
| Regex | Treat "Find" as a regular expression |
| Ignore case | ASCII letters only |
Supported regex syntax: write repeats as {m,n} with n at most 64 instead of *, + and {n,}; no ^, $, \b or nested repeats; ASCII only, and a match is at most 512 bytes. In the replacement use $1 to $9 for groups and $$ for a literal $.
Force HTTPS
| Setting | Description |
|---|---|
| Include this site's domains | When on, the site's own domains are handled too |
| Extra hosts | Exact hosts or *.example.com (any depth of subdomain, not example.com itself), up to 50 |
Changes http:// links to these hosts into https://.
Rule tester
Enter a path and content type, paste HTML, CSS or JavaScript (up to 256 KiB) and click "Run test". The page shows the rewritten output, how many rules matched and how many replacements were made. The test uses the rules being edited, including unsaved changes, runs on the control plane with the same algorithm as the node, and writes no data.
FAQ
Nothing changed after enabling
Confirm the rules are saved and published. Compressed copies cached before you enabled the rule are rewritten only after they expire or the cache is refreshed; content types not ticked in the site's compression settings are sent to visitors uncompressed.
How do I write paths
/blog/ matches every path under that prefix; /*.html matches whole paths ending in .html; empty means the whole site.
It says the node does not support it
The feature needs nodes upgraded to a supporting version; the publish page lists the nodes that need an upgrade.
Does rewriting touch ordinary text in JavaScript
Link rewriting only handles explicit http:// and https:// links; text replacement acts on whatever you put in "Find", so confirm it with the tester first.