Trim URLs
Configuration
The 'Trim URLs' filter is enabled by specifying:
- Apache:
ModPagespeedEnableFilters trim_urls
- Nginx:
pagespeed EnableFilters trim_urls;
in the configuration file.
Description
This filter trims URLs by resolving them by making them relative to the base
URL for the page. E.g. on http://www.example.com/
,
"http://www.example.com/foo"
would be shortened to
"foo"
. The filter works only on URLs that are the values
specified by src
or href
attributes.
It also trims image URLs in CSS if
rewrite_css
is enabled.
The filter reduces the transfer size of HTML files by shortening most of the URLs. Depending on the HTML file, this filter can significantly reduce the number of bytes transmitted on the network.
Operation
The 'Trim URLs' filter removes unnecessary bytes on the wire. While it's useful for development to fully specify your URLs so that links don't break when things move around, these are bytes that are sent unnecessarily on the wire.
For example, if the HTML document looks like this:
<html> <head> <base href="http://www.example.com/"> </head> <body> <a href="http://www.example.com/bar">Link with long URL</a> <img src="http://www.othersite.example.org/image.jpg"> </body> </html>
Then PageSpeed will rewrite it to:
<html> <head> <base href="http://www.example.com/"> </head> <body> <a href="bar">Link with long URL</a> <img src="//www.othersite.example.org/image.jpg"> </body> </html>
Example
You can see the filter in action at www.modpagespeed.com
on this
example.
Requirements
Only URLs referenced by href
and src
attributes and,
if rewrite_css
is enabled,
URLs in CSS files are rewritten. URLs that occur elsewhere are not altered.
Risks
Trimming URLs is considered medium risk. It can cause problems if it uses the wrong base URL. This can happen, for example, if you serve HTML that will be pasted verbatim into other HTML pages. If URLs are trimmed on the first page, they will be incorrect for the page they are inserted into. For this reason, it is off by default.