2020-03-21 21:03:55 -06:00
---
title: uri (Caddyfile directive)
---
# uri
Manipulates a request's URI. It can strip path prefix/suffix or replace substrings on the whole URI.
This directive is distinct from [`rewrite` ](rewrite ) in that `uri` _partially_ changes the URI, rather than setting it to something completely different as `rewrite` does. While `rewrite` is treated specially as an internal redirect, `uri` is just another handler.
## Syntax
2020-05-17 16:32:12 -04:00
```caddy-d
2021-04-16 13:36:54 -04:00
uri [< matcher > ] strip_prefix|strip_suffix|replace|path_regexp \
2020-03-21 21:03:55 -06:00
< target > \
[< replacement > [< limit > ]]
```
- The first (non-matcher) argument specifies the operation:
- **strip_prefix** strips a prefix from the path, if it has the prefix.
- **strip_suffix** strips a suffix from the path, if it has the suffix.
- **replace** performs a substring replacement across the whole URI.
2021-04-16 13:36:54 -04:00
- **path_regexp** performs a regular expression replacement on the path portion of the URI.
2020-03-21 21:03:55 -06:00
- **< target> ** is the prefix, suffix, or search string/regular expression. If a prefix, the leading forward slash may be omitted, since paths always start with a forward slash.
2021-04-16 13:36:54 -04:00
- **< replacement> ** is the replacement string (only valid with `replace` and `path_regexp` ).
2020-03-21 21:03:55 -06:00
- **< limit> ** is an optional limit to the maximum number of replacements (only valid with `replace` ).
## Examples
2020-03-22 16:14:17 -06:00
Strip `/api` from the beginning of all request paths:
2020-03-21 21:03:55 -06:00
2020-05-17 16:32:12 -04:00
```caddy-d
2020-03-22 16:14:17 -06:00
uri strip_prefix /api
2020-03-21 21:03:55 -06:00
```
Strip `.php` from the end of all request paths:
2020-05-17 16:32:12 -04:00
```caddy-d
2020-03-21 21:03:55 -06:00
uri strip_suffix .php
```
Replace "/docs/" with "/v1/docs/" in any request URI:
2020-05-17 16:32:12 -04:00
```caddy-d
2020-03-21 21:03:55 -06:00
uri replace /docs/ /v1/docs/
```
2021-04-16 13:36:54 -04:00
Collapse all repeated slashes in the request path (but not the request query) to a single slash:
```caddy-d
uri path_regexp /{2,} /
```