Bug 66267 - mod_rewrite returns 403 when a directory with the same name as RewriteRule exists in server's root directory
Summary: mod_rewrite returns 403 when a directory with the same name as RewriteRule ex...
Status: RESOLVED CLOSED
Alias: None
Product: Apache httpd-2
Classification: Unclassified
Component: mod_rewrite (show other bugs)
Version: 2.4.51
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: Apache HTTPD Bugs Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-09-16 02:30 UTC by Sato Kenta
Modified: 2022-09-16 02:48 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sato Kenta 2022-09-16 02:30:03 UTC
I added rewrite rule in httpd.conf as below:

```
RewriteEngine on
RewriteRule .* /test/sorry.html
```

When I access to http://myhostname/foo/bar, rewrite rule works as expected.
(Apache returns "[DocumentRoot]/test/sorry.html" no matter which page is accessed)

However, after creating `/test` directory in my server's root directory, rewrite rule will not work.


You can reproduce this symptom with the following docker commands:

```
# Pull and run httpd docker image
docker run --rm -d --name test_httpd -p 4000:80 docker.io/httpd:2.4.51

# Append rewrite rule to httpd.conf
docker cp test_httpd:/usr/local/apache2/conf/httpd.conf ./
echo $'LoadModule rewrite_module modules/mod_rewrite.so\nRewriteEngine on\nRewriteRule .* /test/sorry.html' >> httpd.conf
docker cp ./httpd.conf test_httpd:/usr/local/apache2/conf/httpd.conf

# Create test HTML page
docker exec test_httpd sh -c "mkdir /usr/local/apache2/htdocs/test && echo Test page > /usr/local/apache2/htdocs/test/sorry.html"
docker restart test_httpd

# This curl command works as expected. (says "Test page" with status 200)
curl http://localhost:4000/foo/bar

# After creating `/test` directory in my root directory in container, apache returns "403 Forbidden".
docker exec test_httpd sh -c "mkdir /test"
curl http://localhost:4000/foo/bar
docker stop test_httpd
```

This example is a container, but the same thing happens on a normal server.
Comment 1 Eric Covener 2022-09-16 02:35:06 UTC
Rewrite guesses that you are rewriting a url path to a filesystem path when the first segment exists on disk.

When rewiring from url to url, PT is a good way to make it explicit.

This guessing is a historical behavior that is difficult to unwind.
Comment 2 Sato Kenta 2022-09-16 02:48:31 UTC
Thank you for answering. I understood.
I'm sorry for not reading the documentation carefully.