filter · HTML filters
Trim URLs (left_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
- An alternate spelling of
trim_urls: the module accepts both names for the same filter. - Not in CoreFilters: it runs only when you enable it by name.
- Risk rating on these docs: Generally safe.
Risks
- 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 left_trim_urlsnginx
pagespeed EnableFilters left_trim_urls;IIS (pagespeed.config)
pagespeed EnableFilters left_trim_urls
Scoping, ForbidFilters and the thresholds filters read:
Choosing filters
.
Frequently asked questions
- What is the left_trim_urls filter?
left_trim_urlsis an alternate spelling oftrim_urlsthat the module accepts. Both names enable the same filter.- Is left_trim_urls enabled by default?
- No.
left_trim_urlsis not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name withEnableFilters. - How do I enable left_trim_urls on Apache and nginx?
- Add
ModPagespeedEnableFilters left_trim_urlson Apache orpagespeed EnableFilters left_trim_urls;on nginx. On IIS, addpagespeed EnableFilters left_trim_urlsto pagespeed.config.
Related filters
- trim_urls — Trim URLs
- add_base_tag — Add base tag
- add_head — Add head
- add_ids — Add IDs
- add_instrumentation — Add instrumentation
- collapse_whitespace — Collapse whitespace
- combine_heads — Combine heads
- compute_statistics — Compute HTML statistics
This page is drawn from the left_trim_urls entry on HTML filters. Every filter in one table: PageSpeed filters.