Repository: caddyserver/caddy (language: go)

Issue:
Caddy 2.6.x - handle_path behavior change in relation to php_fastcgi
Hello, 

i was testing caddy 2.6 to deploy on my home network, and i noticed that using the same config for 2.5 and 2.6 no longer works correctly for `handle` and `handle_path`

For example this Caddyfile works for 2.5 and produce correct response.

```caddyfile
{
	http_port 8084
	auto_https off
	log stdout
	grace_period 10s
	debug
	admin localhost:2018
}

:8084 {
	log
	route {
		handle_path /app* {
			uri strip_prefix /app
			root * /home/user/tests/test_caddy/test

			php_fastcgi unix//var/run/php/php8.1-fpm.sock {
			}

			file_server
		}
	}
}

```

However using the same config for 2.6 produce 308 for `/app` `/app/`, we have to use `/app/index.php` to get the app to work, otherwise `308` response is returned 

# Logs 

v2.6.1 h1:EDqo59TyYWhXQnfde93Mmv4FJfYe00dO60zMiEt+pzo=

```json
[
    {
        "request": {
            "remote_ip": "127.0.0.1",
            "remote_port": "55046",
            "proto": "HTTP/1.1",
            "method": "GET",
            "host": "localhost:8084",
            "uri": "/app",
            "headers": {
                "User-Agent": [
                    "HTTPie/2.6.0"
                ],
                "Accept-Encoding": [
                    "gzip, deflate"
                ],
                "Accept": [
                    "*/*"
                ],
                "Connection": [
                    "keep-alive"
                ]
            }
        },
        "method": "GET",
        "uri": "/"
    },
    {
        "request": {
            "remote_ip": "127.0.0.1",
            "remote_port": "55046",
            "proto": "HTTP/1.1",
            "method": "GET",
            "host": "localhost:8084",
            "uri": "/",
            "headers": {
                "User-Agent": [
                    "HTTPie/2.6.0"
                ],
                "Accept-Encoding": [
                    "gzip, deflate"
                ],
                "Accept": [
                    "*/*"
                ],
                "Connection": [
                    "keep-alive"
                ]
            }
        },
        "method": "GET",
        "uri": "."
    },
    {
        "request": {
            "remote_ip": "127.0.0.1",
            "remote_port": "55046",
            "proto": "HTTP/1.1",
            "method": "GET",
            "host": "localhost:8084",
            "uri": "/app",
            "headers": {
                "Accept": [
                    "*/*"
                ],
                "Connection": [
                    "keep-alive"
                ],
                "User-Agent": [
                    "HTTPie/2.6.0"
                ],
                "Accept-Encoding": [
                    "gzip, deflate"
                ]
            }
        },
        "user_id": "",
        "duration": 0.00009701,
        "size": 0,
        "status": 308,
        "resp_headers": {
            "Server": [
                "Caddy"
            ],
            "Location": [
                "./"
            ],
            "Content-Type": []
        }
    },
    {
        "request": {
            "remote_ip": "127.0.0.1",
            "remote_port": "55052",
            "proto": "HTTP/1.1",
            "method": "GET",
            "host": "localhost:8084",
            "uri": "/app/",
            "headers": {
                "User-Agent": [
                    "HTTPie/2.6.0"
                ],
                "Accept-Encoding": [
                    "gzip, deflate"
                ],
                "Accept": [
                    "*/*"
                ],
                "Connection": [
                    "keep-alive"
                ]
            }
        },
        "method": "GET",
        "uri": "/"
    },
    {
        "request": {
            "remote_ip": "127.0.0.1",
            "remote_port": "55052",
            "proto": "HTTP/1.1",
            "method": "GET",
            "host": "localhost:8084",
            "uri": "/app/",
            "headers": {
                "User-Agent": [
                    "HTTPie/2.6.0"
                ],
                "Accept-Encoding": [
                    "gzip, deflate"
                ],
                "Accept": [
                    "*/*"
                ],
                "Connection": [
                    "keep-alive"
                ]
            }
        },
        "user_id": "",
        "duration": 0.000093239,
        "size": 0,
        "status": 308,
        "resp_headers": {
            "Location": [
                "/"
            ],
            "Content-Type": [],
            "Server": [
                "Caddy"
            ]
        }
    },
    {
        "request": {
            "remote_ip": "127.0.0.1",
            "remote_port": "55058",
            "proto": "HTTP/1.1",
            "method": "GET",
            "host": "localhost:8084",
            "uri": "/app/index.php",
            "headers": {
                "User-Agent": [
                    "HTTPie/2.6.0"
                ],
                "Accept-Encoding": [
                    "gzip, deflate"
                ],
                "Accept": [
                    "*/*"
                ],
                "Connection": [
                    "keep-alive"
                ]
            }
        },
        "method": "GET",
        "uri": "/index.php"
    },
    {
        "dial": "/var/run/php/php8.1-fpm.sock",
        "total_upstreams": 1
    },
    {
        "request": {
            "remote_ip": "127.0.0.1",
            "remote_port": "55058",
            "proto": "HTTP/1.1",
            "method": "GET",
            "host": "localhost:8084",
            "uri": "/index.php",
            "headers": {
                "Accept": [
                    "*/*"
                ],
                "X-Forwarded-For": [
                    "127.0.0.1"
                ],
                "User-Agent": [
                    "HTTPie/2.6.0"
                ],
                "Accept-Encoding": [
                    "gzip, deflate"
                ],
                "X-Forwarded-Proto": [
                    "http"
                ],
                "X-Forwarded-Host": [
                    "localhost:8084"
                ]
            }
        },
        "env": {
            "REQUEST_URI": "/app/index.php",
            "HTTP_ACCEPT": "*/*",
            "GATEWAY_INTERFACE": "CGI/1.1",
            "REMOTE_HOST": "127.0.0.1",
            "REMOTE_USER": "",
            "REQUEST_SCHEME": "http",
            "HTTP_HOST": "localhost:8084",
            "SERVER_PORT": "8084",
            "PATH_INFO": "",
            "REMOTE_PORT": "55058",
            "SERVER_NAME": "localhost",
            "HTTP_X_FORWARDED_FOR": "127.0.0.1",
            "REMOTE_ADDR": "127.0.0.1",
            "REMOTE_IDENT": "",
            "HTTP_ACCEPT_ENCODING": "gzip, deflate",
            "HTTP_X_FORWARDED_HOST": "localhost:8084",
            "AUTH_TYPE": "",
            "SCRIPT_FILENAME": "/home/user/tests/test_caddy/test/index.php",
            "HTTP_USER_AGENT": "HTTPie/2.6.0",
            "DOCUMENT_ROOT": "/home/user/tests/test_caddy/test",
            "REQUEST_METHOD": "GET",
            "SERVER_PROTOCOL": "HTTP/1.1",
            "CONTENT_LENGTH": "",
            "DOCUMENT_URI": "/index.php",
            "QUERY_STRING": "",
            "SERVER_SOFTWARE": "Caddy/v2.6.1",
            "SCRIPT_NAME": "/index.php",
            "HTTP_X_FORWARDED_PROTO": "http",
            "CONTENT_TYPE": ""
        },
        "dial": "/var/run/php/php8.1-fpm.sock",
        "env": {
            "GATEWAY_INTERFACE": "CGI/1.1",
            "REQUEST_URI": "/app/index.php",
            "HTTP_ACCEPT": "*/*",
            "PATH_INFO": "",
            "REMOTE_HOST": "127.0.0.1",
            "REMOTE_USER": "",
            "REQUEST_SCHEME": "http",
            "HTTP_HOST": "localhost:8084",
            "SERVER_PORT": "8084",
            "REMOTE_ADDR": "127.0.0.1",
            "REMOTE_PORT": "55058",
            "SERVER_NAME": "localhost",
            "HTTP_X_FORWARDED_FOR": "127.0.0.1",
            "AUTH_TYPE": "",
            "REMOTE_IDENT": "",
            "HTTP_ACCEPT_ENCODING": "gzip, deflate",
            "HTTP_X_FORWARDED_HOST": "localhost:8084",
            "DOCUMENT_ROOT": "/home/user/tests/test_caddy/test",
            "SCRIPT_FILENAME": "/home/user/tests/test_caddy/test/index.php",
            "HTTP_USER_AGENT": "HTTPie/2.6.0",
            "CONTENT_LENGTH": "",
            "REQUEST_METHOD": "GET",
            "SERVER_PROTOCOL": "HTTP/1.1",
            "QUERY_STRING": "",
            "DOCUMENT_URI": "/index.php",
            "CONTENT_TYPE": "",
            "SERVER_SOFTWARE": "Caddy/v2.6.1",
            "SCRIPT_NAME": "/index.php",
            "HTTP_X_FORWARDED_PROTO": "http"
        },
        "request": {
            "remote_ip": "127.0.0.1",
            "remote_port": "55058",
            "proto": "HTTP/1.1",
            "method": "GET",
            "host": "localhost:8084",
            "uri": "/index.php",
            "headers": {
                "Accept": [
                    "*/*"
                ],
                "X-Forwarded-For": [
                    "127.0.0.1"
                ],
                "User-Agent": [
                    "HTTPie/2.6.0"
                ],
                "Accept-Encoding": [
                    "gzip, deflate"
                ],
                "X-Forwarded-Proto": [
                    "http"
                ],
                "X-Forwarded-Host": [
                    "localhost:8084"
                ]
            }
        }
    },
    {
        "upstream": "unix//var/run/php/php8.1-fpm.sock",
        "duration": 0.009203193,
        "request": {
            "remote_ip": "127.0.0.1",
            "remote_port": "55058",
            "proto": "HTTP/1.1",
            "method": "GET",
            "host": "localhost:8084",
            "uri": "/index.php",
            "headers": {
                "User-Agent": [
                    "HTTPie/2.6.0"
                ],
                "Accept-Encoding": [
                    "gzip, deflate"
                ],
                "X-Forwarded-Proto": [
                    "http"
                ],
                "X-Forwarded-Host": [
                    "localhost:8084"
                ],
                "Accept": [
                    "*/*"
                ],
                "X-Forwarded-For": [
                    "127.0.0.1"
                ]
            }
        },
        "headers": {
            "Content-Type": [
                "text/html; charset=UTF-8"
            ]
        },
        "status": 200
    },
    {
        "request": {
            "remote_ip": "127.0.0.1",
            "remote_port": "55058",
            "proto": "HTTP/1.1",
            "method": "GET",
            "host": "localhost:8084",
            "uri": "/app/index.php",
            "headers": {
                "Accept-Encoding": [
                    "gzip, deflate"
                ],
                "Accept": [
                    "*/*"
                ],
                "Connection": [
                    "keep-alive"
                ],
                "User-Agent": [
                    "HTTPie/2.6.0"
                ]
            }
        },
        "user_id": "",
        "duration": 0.00955324,
        "size": 87843,
        "status": 200,
        "resp_headers": {
            "Server": [
                "Caddy"
            ],
            "Content-Type": [
                "text/html; charset=UTF-8"
            ]
        }
    }
]
```



Discussion / hints:
I was able to somewhat fix the issue by changing the config to 

```caddyfile
:8084 {
	log
	route {
		redir /app /app/
		handle_path /app/* {
			root * /home/user/tests/test_caddy/test

			php_fastcgi unix//var/run/php/php8.1-fpm.sock {
			}

			file_server
		}
	}
}
```

If you feel this is not an issue, please feel free to close it i thought I'd share the issues i had upgrading.
Thanks for letting us know!

> uri strip_prefix /app

That was redundant, since `handle_path` would do this for you.

What exactly breaks though? 

> redir /app /app/

Why does that fix something?

I guess I don't really understand. Some of the log entries don't make much sense as they don't look like full log lines, e.g. missing some response information (bad nesting maybe?) ...
> Thanks for letting us know!
> 
> > uri strip_prefix /app
> 
> That was redundant, since `handle_path` would do this for you.
> 
> What exactly breaks though?
> 
> > redir /app /app/
> 
> Why does that fix something?
> 
> I guess I don't really understand. Some of the log entries don't make much sense as they don't look like full log lines, e.g. missing some response information (bad nesting maybe?) ...

Hi thanks for the quick reply, i copied the logs as they are from caddy using both `debug` and `log` as it is in the config up.

i am not quite sure why `handle_path /app*` is not working correctly, once i made the changes i maintained up there it started working it seems there is a significances for `/` at the end of `handle_path` now compared to before. let me know if you need further debugging


Edit:

It seems `uri strip_prefix /app` nested under `handle_path` was causing the problem. using this config 

```caddyfile
:8084 {
	log
	route {
		handle_path /app* {
			root * /home/user/tests/test_caddy/test
			php_fastcgi unix//var/run/php/php8.1-fpm.sock
			file_server
		}
	}
}
```
does work for `/app/` however `/app` still redirect to `/` for some reason.

### logs
for both `/app` and `/app/`
```
2022/09/23 17:08:34.131 DEBUG   http.handlers.rewrite   rewrote request {"request": {"remote_ip": "127.0.0.1", "remote_port": "59018", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8084", "uri": "/app", "headers": {"User-Agent": ["HTTPie/2.6.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "Connection": ["keep-alive"]}}, "method": "GET", "uri": "/"}
2022/09/23 17:08:34.131 INFO    http.log.access handled request {"request": {"remote_ip": "127.0.0.1", "remote_port": "59018", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8084", "uri": "/app", "headers": {"User-Agent": ["HTTPie/2.6.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "Connection": ["keep-alive"]}}, "user_id": "", "duration": 0.000131509, "size": 0, "status": 308, "resp_headers": {"Server": ["Caddy"], "Location": ["/"], "Content-Type": []}}
2022/09/23 17:08:36.503 DEBUG   http.handlers.rewrite   rewrote request {"request": {"remote_ip": "127.0.0.1", "remote_port": "59030", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8084", "uri": "/app/", "headers": {"User-Agent": ["HTTPie/2.6.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "Connection": ["keep-alive"]}}, "method": "GET", "uri": "/"}
2022/09/23 17:08:36.503 DEBUG   http.handlers.rewrite   rewrote request {"request": {"remote_ip": "127.0.0.1", "remote_port": "59030", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8084", "uri": "/", "headers": {"User-Agent": ["HTTPie/2.6.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "Connection": ["keep-alive"]}}, "method": "GET", "uri": "/index.php"}
2022/09/23 17:08:36.503 DEBUG   http.handlers.reverse_proxy     selected upstream       {"dial": "/var/run/php/php8.1-fpm.sock", "total_upstreams": 1}
2022/09/23 17:08:36.503 DEBUG   http.reverse_proxy.transport.fastcgi    roundtrip       {"request": {"remote_ip": "127.0.0.1", "remote_port": "59030", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8084", "uri": "/index.php", "headers": {"User-Agent": ["HTTPie/2.6.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "X-Forwarded-For": ["127.0.0.1"], "X-Forwarded-Proto": ["http"], "X-Forwarded-Host": ["localhost:8084"]}}, "env": {"SERVER_SOFTWARE": "Caddy/v2.6.1", "DOCUMENT_URI": "/index.php", "SERVER_PORT": "8084", "HTTP_X_FORWARDED_PROTO": "http", "CONTENT_TYPE": "", "REMOTE_IDENT": "", "GATEWAY_INTERFACE": "CGI/1.1", "AUTH_TYPE": "", "REMOTE_PORT": "59030", "REQUEST_SCHEME": "http", "DOCUMENT_ROOT": "/home/user/tests/test_caddy/test", "SCRIPT_NAME": "/index.php", "REMOTE_ADDR": "127.0.0.1", "SERVER_PROTOCOL": "HTTP/1.1", "HTTP_HOST": "localhost:8084", "SCRIPT_FILENAME": "/home/user/tests/test_caddy/test/index.php", "HTTP_X_FORWARDED_HOST": "localhost:8084", "CONTENT_LENGTH": "", "REMOTE_HOST": "127.0.0.1", "REMOTE_USER": "", "REQUEST_METHOD": "GET", "SERVER_NAME": "localhost", "HTTP_ACCEPT_ENCODING": "gzip, deflate", "HTTP_USER_AGENT": "HTTPie/2.6.0", "PATH_INFO": "", "HTTP_ACCEPT": "*/*", "QUERY_STRING": "", "HTTP_X_FORWARDED_FOR": "127.0.0.1", "REQUEST_URI": "/app/"}, "dial": "/var/run/php/php8.1-fpm.sock", "env": {"QUERY_STRING": "", "HTTP_ACCEPT": "*/*", "REQUEST_URI": "/app/", "HTTP_X_FORWARDED_FOR": "127.0.0.1", "HTTP_X_FORWARDED_PROTO": "http", "CONTENT_TYPE": "", "SERVER_SOFTWARE": "Caddy/v2.6.1", "DOCUMENT_URI": "/index.php", "SERVER_PORT": "8084", "REMOTE_IDENT": "", "AUTH_TYPE": "", "GATEWAY_INTERFACE": "CGI/1.1", "SCRIPT_NAME": "/index.php", "REMOTE_ADDR": "127.0.0.1", "REMOTE_PORT": "59030", "REQUEST_SCHEME": "http", "DOCUMENT_ROOT": "/home/user/tests/test_caddy/test", "HTTP_X_FORWARDED_HOST": "localhost:8084", "CONTENT_LENGTH": "", "SERVER_PROTOCOL": "HTTP/1.1", "HTTP_HOST": "localhost:8084", "SCRIPT_FILENAME": "/home/user/tests/test_caddy/test/index.php", "SERVER_NAME": "localhost", "HTTP_ACCEPT_ENCODING": "gzip, deflate", "HTTP_USER_AGENT": "HTTPie/2.6.0", "PATH_INFO": "", "REMOTE_HOST": "127.0.0.1", "REMOTE_USER": "", "REQUEST_METHOD": "GET"}, "request": {"remote_ip": "127.0.0.1", "remote_port": "59030", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8084", "uri": "/index.php", "headers": {"Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "X-Forwarded-For": ["127.0.0.1"], "X-Forwarded-Proto": ["http"], "X-Forwarded-Host": ["localhost:8084"], "User-Agent": ["HTTPie/2.6.0"]}}}
2022/09/23 17:08:36.503 DEBUG   http.handlers.reverse_proxy     upstream roundtrip      {"upstream": "unix//var/run/php/php8.1-fpm.sock", "duration": 0.000366592, "request": {"remote_ip": "127.0.0.1", "remote_port": "59030", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8084", "uri": "/index.php", "headers": {"User-Agent": ["HTTPie/2.6.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "X-Forwarded-For": ["127.0.0.1"], "X-Forwarded-Proto": ["http"], "X-Forwarded-Host": ["localhost:8084"]}}, "headers": {"Content-Type": ["text/html; charset=UTF-8"]}, "status": 200}
2022/09/23 17:08:36.503 INFO    http.log.access handled request {"request": {"remote_ip": "127.0.0.1", "remote_port": "59030", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8084", "uri": "/app/", "headers": {"User-Agent": ["HTTPie/2.6.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "Connection": ["keep-alive"]}}, "user_id": "", "duration": 0.000566754, "size": 3, "status": 200, "resp_headers": {"Content-Type": ["text/html; charset=UTF-8"], "Server": ["Caddy"]}}
```
is `/home/user/tests/test_caddy/test/app` a directory?

If so, I think that's normal/expected to redirect to `/app/`. The file server enforces canonical paths, otherwise relative links in HTML don't work.

Edit: oh, it redirects `/app` to `/`? Is that different from what you said earlier? Or what was happening before?
> 
`/home/user/tests/test_caddy/test/` is the directory containing `index.php`, and it's not actually redirecting to `/app/` as i noticed, it's redirecting to `/` 

```
2022/09/23 17:08:34.131 INFO    http.log.access handled request {"request": {"remote_ip": "127.0.0.1", "remote_port": "59018", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8084", "uri": "/app", "headers": {"User-Agent": ["HTTPie/2.6.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "Connection": ["keep-alive"]}}, "user_id": "", "duration": 0.000131509, "size": 0, "status": 308, "resp_headers": {"Server": ["Caddy"], "Location": ["/"], "Content-Type": []}}
```

redirecting `/app` to `/app/` is the old behavior in 2.5 i think, thats why i didn't notice it before upgrading to 2.6


Fix patch:
diff --git a/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go b/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go
index a9e6b220f9d..799050e9701 100644
--- a/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go
+++ b/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go
@@ -348,7 +348,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
 		}
 		redirHandler := caddyhttp.StaticResponse{
 			StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
-			Headers:    http.Header{"Location": []string{"{http.request.uri.path}/"}},
+			Headers:    http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
 		}
 		redirRoute := caddyhttp.Route{
 			MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},
diff --git a/modules/caddyhttp/rewrite/rewrite.go b/modules/caddyhttp/rewrite/rewrite.go
index 95bc50468e1..31c97788679 100644
--- a/modules/caddyhttp/rewrite/rewrite.go
+++ b/modules/caddyhttp/rewrite/rewrite.go
@@ -383,8 +383,13 @@ func trimPathPrefix(escapedPath, prefix string) string {
 		iPrefix++
 	}
 
-	// found matching prefix, trim it
-	return escapedPath[iPath:]
+	// if we iterated through the entire prefix, we found it, so trim it
+	if iPath >= len(prefix) {
+		return escapedPath[iPath:]
+	}
+
+	// otherwise we did not find the prefix
+	return escapedPath
 }
 
 func reverse(s string) string {


Test patch:
diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.txt b/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.txt
index bb7c7f7b9fc..2fa2b79797e 100644
--- a/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.txt
+++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.txt
@@ -8,7 +8,7 @@ route {
 		}
 		not path */
 	}
-	redir @canonicalPath {path}/ 308
+	redir @canonicalPath {http.request.orig_uri.path}/ 308
 
 	# If the requested file does not exist, try index files
 	@indexFiles {
@@ -50,7 +50,7 @@ route {
 													"handler": "static_response",
 													"headers": {
 														"Location": [
-															"{http.request.uri.path}/"
+															"{http.request.orig_uri.path}/"
 														]
 													},
 													"status_code": 308
diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.txt b/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.txt
index dbb08719f7b..70a0780d110 100644
--- a/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.txt
+++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.txt
@@ -42,7 +42,7 @@
 									"handler": "static_response",
 									"headers": {
 										"Location": [
-											"{http.request.uri.path}/"
+											"{http.request.orig_uri.path}/"
 										]
 									},
 									"status_code": 308
diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.txt b/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.txt
index 488c525283b..d90b2cb4e2b 100644
--- a/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.txt
+++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.txt
@@ -30,7 +30,7 @@ php_fastcgi @api localhost:9000
 													"handler": "static_response",
 													"headers": {
 														"Location": [
-															"{http.request.uri.path}/"
+															"{http.request.orig_uri.path}/"
 														]
 													},
 													"status_code": 308
diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.txt b/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.txt
index 90d1633b437..a04d66fe42f 100644
--- a/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.txt
+++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.txt
@@ -43,7 +43,7 @@ php_fastcgi localhost:9000 {
 									"handler": "static_response",
 									"headers": {
 										"Location": [
-											"{http.request.uri.path}/"
+											"{http.request.orig_uri.path}/"
 										]
 									},
 									"status_code": 308
diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.txt b/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.txt
index 43427862775..a3381f675e8 100644
--- a/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.txt
+++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.txt
@@ -46,7 +46,7 @@ php_fastcgi localhost:9000 {
 									"handler": "static_response",
 									"headers": {
 										"Location": [
-											"{http.request.uri.path}/"
+											"{http.request.orig_uri.path}/"
 										]
 									},
 									"status_code": 308
diff --git a/modules/caddyhttp/rewrite/rewrite_test.go b/modules/caddyhttp/rewrite/rewrite_test.go
index bc20c853547..5875983b6da 100644
--- a/modules/caddyhttp/rewrite/rewrite_test.go
+++ b/modules/caddyhttp/rewrite/rewrite_test.go
@@ -225,6 +225,16 @@ func TestRewrite(t *testing.T) {
 			input:  newRequest(t, "GET", "/prefix/foo/bar"),
 			expect: newRequest(t, "GET", "/foo/bar"),
 		},
+		{
+			rule:   Rewrite{StripPathPrefix: "/prefix"},
+			input:  newRequest(t, "GET", "/prefix"),
+			expect: newRequest(t, "GET", ""),
+		},
+		{
+			rule:   Rewrite{StripPathPrefix: "/prefix"},
+			input:  newRequest(t, "GET", "/"),
+			expect: newRequest(t, "GET", "/"),
+		},
 		{
 			rule:   Rewrite{StripPathPrefix: "/prefix"},
 			input:  newRequest(t, "GET", "/prefix/foo%2Fbar"),