# Trim URLs (trim_urls)

Shortens URLs relative to base URL. How the mod_pagespeed trim_urls filter works, when to use it, its risks and the Apache, nginx and IIS directives.

Canonical URL: https://modpagespeed.com/docs/filters/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](https://modpagespeed.com/examples/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_urls` for that content.

- Verify with the `X-Mod-Pagespeed` response header and a `?PageSpeedFilters=-trim_urls` comparison; [Is it working?](https://modpagespeed.com/docs/is-it-working/) has the steps.

## Configuration

Enable it in the module configuration, at server, virtual-host or location scope:

**Apache**

```apache
ModPagespeedEnableFilters trim_urls
```

**nginx**

```nginx
pagespeed EnableFilters trim_urls;
```

**IIS (pagespeed.config)**

```text
pagespeed EnableFilters trim_urls
```

```html
<!-- 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](https://modpagespeed.com/docs/filter-selection/).

## Live example

See the before and after, with the source diff: [trim_urls example](https://modpagespeed.com/examples/trim_urls/).

## Frequently asked questions

**What does the trim_urls filter do?**

`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.

**Is trim_urls enabled by default?**

No. `trim_urls` is not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name with `EnableFilters`.

**How do I enable trim_urls on Apache and nginx?**

Add `ModPagespeedEnableFilters trim_urls` on Apache or `pagespeed EnableFilters trim_urls;` on nginx. On IIS, add `pagespeed EnableFilters trim_urls` to pagespeed.config.

## Related filters

- [`left_trim_urls`](https://modpagespeed.com/docs/filters/left_trim_urls/): Trim URLs
- [`rewrite_domains`](https://modpagespeed.com/docs/filters/rewrite_domains/): Rewrite domains
- [`extend_cache`](https://modpagespeed.com/docs/filters/extend_cache/): Extend cache
- [`add_base_tag`](https://modpagespeed.com/docs/filters/add_base_tag/): Add base tag
- [`add_head`](https://modpagespeed.com/docs/filters/add_head/): Add head
- [`add_ids`](https://modpagespeed.com/docs/filters/add_ids/): Add IDs
- [`add_instrumentation`](https://modpagespeed.com/docs/filters/add_instrumentation/): Add instrumentation
- [`collapse_whitespace`](https://modpagespeed.com/docs/filters/collapse_whitespace/): Collapse whitespace

This page is drawn from the [trim_urls entry on HTML filters](https://modpagespeed.com/docs/html-filters/#trim_urls). Every filter in one table: [PageSpeed filters](https://modpagespeed.com/docs/filters/).
