Home > Net >  mod rewrite ignores digits in condition, how to make it take digits into account?
mod rewrite ignores digits in condition, how to make it take digits into account?

Time:12-03

All is done inside httpd.conf, there is no .htaccess

I have two simple conditions. If url has in it the string "talis" direct to one file, otherwise to another.

RewriteRule talis talis.php [L]
RewriteRule . index.php [L]

This piece works as expected [domain]/t/1/talis/s goes to talis.php [domain]/t/1/bob/s goes to index.php

But when I try to put numbers in there, it always defaults to the last rule

RewriteRule talis01 talis.php [L]
RewriteRule . index.php [L]

(notice talis01)

This will always go to the second condition.
[domain]/t/1/talis01/s goes to index.php
[domain]/t/1/bob/s goes to index.php

CodePudding user response:

Since you are using a relative substitution string then I assume these are in a directory context (perhaps .htaccess), as opposed to a server (or virtualhost) context. And the dot in the second rule's pattern is also intended for a directory context.

mod_rewrite isn't "ignoring the digits". The request is rewritten to talis.php by the first rule, but then talis.php is being rewritten by the second rule to index.php.

In more detail...

RewriteRule talis talis.php [L]
RewriteRule . index.php [L]

In the first example, a request that contains talis is rewritten to talis.php and the L flag causes the rewrite engine to start over. On the second pass talis.php also matches the same rule and is rewritten to talis.php again. Since the URL is unchanged, processing stops and talis.php accepts the request.

If it was not for the fact that the first rule also matches talis.php (the URL that the first rule rewrites to) then this too would result in the request being rewritten to index.php.

RewriteRule talis01 talis.php [L]
RewriteRule . index.php [L]

In the second example, a request that contains talis01 is first rewritten to talis.php and the L flag causes the rewrite engine to start over. talis.php does not match the first rule, but it does match the second rule so the request is rewritten a second time to index.php. The L flag causes the rewrite engine to start over. The second rule matches (again), the URL is unchanged so processing stops and index.php accepts the request.

Assuming you are on Apache 2.4 then the easiest solution is to use the END flag on the first rule, instead of L. This stops all processing by the rewrite engine and prevents another loop.

For example:

RewriteRule talis01 talis.php [END]
RewriteRule . index.php [L]

Although, ordinarily you would use a more restrictive regex and/or conditions to ensure that rewritten requests are not rematched by any of the directives (if that is not the intention). eg. It is unusual to see the second rule without any conditions since this literally rewrites everything to index.php, including requests to index.php itself.

  • Related