Clean URLs in IIS with mod_rewrite
-
25-09-2019 - |
Question
I have a mod_rewrite issue. Or more accurately, I have an issue with ISAPI_Rewrite, which is a mod_rewrite clone for IIS. Specifically, ISAPI_Rewrite v3 running on IIS 7. It aims to behave identically to mod_rewrite, so I hope my problem applies to mod_rewrite as well.
My problem boils down to this: I have a script bar.php
which I want to access via the URL foo
. I first rewrite foo
to bar
. Then try to convert the clean URL to a real file name.
RewriteRule ^foo$ bar
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php
The RewriteCond
is failing because ${REQUEST_FILENAME}
still says "foo" rather than "bar". It is not updated after the first rewrite.
How can I make this work? I could fix this by simply writing RewriteRule ^foo$ bar.php
. But I would strongly prefer to keep the URL remapping rules separate from the file extension rules.
Solution
Tentative answer...
RewriteCond %{DOCUMENT_ROOT}/$0.php -f
By using $0
I get access to the latest rewritten URL, which makes me happy. But I have to add %{DOCUMENT_ROOT}
to turn that into a file name, which makes me sad. Bit of a kludge.