filter · HTML filters
Trim URLs (trim_urls)
- Category:
- HTML
- CoreFilters:
- No
- OptimizeForBandwidth:
- No
- Risk:
- Generally safe
trim_urls shortens URLs inside the page by stripping the parts that repeat the page’s own origin. An absolute URL whose scheme, host and port match the page loses its origin, and the page’s own directory prefix is trimmed too; URLs on any other origin, including the same host over another scheme, are left untouched. left_trim_urls is an accepted alternate spelling of the same filter; both names switch on the same code.
How it works
Live demo: trim_urls.
When it helps and when it does not
It saves a few bytes per URL on pages dense with same-origin absolute links, which is typical of CMS output that expands every URL in full; under gzip or Brotli the saving shrinks further, since the repeated origin strings compress well. It does not help pages that already use relative URLs throughout. Keep it off for HTML that lives beyond its origin: a saved page, an emailed copy, or markup served under a second domain resolves relative URLs against the wrong base.
How it decides
Each URL-valued attribute is resolved against the page’s base URL, and a <base> tag wins when present and is never itself rewritten. Only what matches gets trimmed: a full origin match drops the origin, a path under the page’s own directory then drops that directory as well, and a URL on another origin keeps its full form. A trim is kept only when the shorter URL resolves back to exactly the original one.
When to use it
- Not in CoreFilters: it runs only when you enable it by name.
- Risk rating on these docs: Generally safe.
Risks
-
Serving the same cached HTML from multiple domains, or any flow that detaches the markup from its origin, turns the trimming into broken links; disable
trim_urlsfor that content. -
Verify with the
X-Mod-Pagespeedresponse header and a?PageSpeedFilters=-trim_urlscomparison; Is it working? has the steps.
Configuration
Enable it in the module configuration, at server, virtual-host or location scope:
Apache
ModPagespeedEnableFilters trim_urlsnginx
pagespeed EnableFilters trim_urls;IIS (pagespeed.config)
pagespeed EnableFilters trim_urls<!-- page: https://example.com/shop/ -->
<!-- before -->
<a href="https://example.com/shop/cart">Cart</a>
<img src="https://example.com/img/logo.png" />
<!-- after -->
<a href="cart">Cart</a>
<img src="/img/logo.png" />
Scoping, ForbidFilters and the thresholds filters read:
Choosing filters
.
Live example
Shortens resource URLs to relative form where it is safe to do so.
Measured on the demo page, original against optimized:
-
HTML size
147 B → 124 B −16%
Frequently asked questions
- What does the trim_urls filter do?
trim_urlsshortens URLs inside the page by stripping the parts that repeat the page's own origin. An absolute URL whose scheme, host and port match the page loses its origin, and the page's own directory prefix is trimmed too; URLs on any other origin, including the same host over another scheme, are left untouched.left_trim_urlsis an accepted alternate spelling of the same filter; both names switch on the same code.- Is trim_urls enabled by default?
- No.
trim_urlsis not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name withEnableFilters. - How do I enable trim_urls on Apache and nginx?
- Add
ModPagespeedEnableFilters trim_urlson Apache orpagespeed EnableFilters trim_urls;on nginx. On IIS, addpagespeed EnableFilters trim_urlsto pagespeed.config.
Related filters
- left_trim_urls — Trim URLs
- rewrite_domains — Rewrite domains
- extend_cache — Extend cache
- add_base_tag — Add base tag
- add_head — Add head
- add_ids — Add IDs
- add_instrumentation — Add instrumentation
- collapse_whitespace — Collapse whitespace
This page is drawn from the trim_urls entry on HTML filters. Every filter in one table: PageSpeed filters.