Rewrite rule & escaping (encoded) forward slashes

2019-07-22 13:49发布

问题:

I have developed this RewriteRule which has been working great, up until I discovered an issue.

This is the rule here:

 RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/([^/]+)/(.*)$
 RewriteCond %{DOCUMENT_ROOT}/%1/%2/%4.php -f
 RewriteRule ^ /%1/%2/%4.php?id=%3 [QSA,L]

The URL format is like so: /home/service/ID/scriptname note that ID is dynamic and the RewriteRule does not take not of it when checking for the files existence.

But in this one scenario I found out, one of the IDs I'm using contains a '%2F' - which when decoded is translated to a forward slash (/) - And for some reason, the Rewrite rule when breaks because it thinks there's another directory.

My question is, why is %2F being decoded within the RewriteRule, and how can I engineer a solution? - Preferably other than a quick hack or changing the ID.

回答1:

I think you need to add the B flag in the rewrite rule, like this:

RewriteRule ^ /%1/%2/%4.php?id=%3 [QSA,L,B]

Check this link for more information.