Petar Kolev
Petar Kolev

Reputation: 39

htaccess int:tolower redirected many times

i have activate RewriteMap in Virtualhost with:

RewriteEngine On
RewriteMap lc int:tolower

... and after that in htacess have this role:

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]

... but make me redirected many times and don't get my REQUEST_URI every time redirect me to /.

I don't have idea why don't get it (on my old hosting work fine) if anyone has an idea and can help? I will be very happy and thank you in advance!

Upvotes: 1

Views: 31

Answers (1)

MrWhite
MrWhite

Reputation: 46012

There's nothing actually "wrong" with the rule itself, however, I suspect you have a conflict with other directives that internally rewrite the URL (that may occur later in the file). The REQUEST_URI server variable is modified when the URL is rewritten - so does not necessarily contain the originally requested URL on subsequent passes through the rewrite engine.

To ensure the rule is only processed on the initial request and not the rewritten request we can check against the REDIRECT_STATUS environment variable, which is empty on the initial request and set to (string)200 (as in 200 OK status) after the first successful rewrite.

For example:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule [A-Z] ${lc:%{REQUEST_URI}} [R=301,L]

Your original condition that checked against REQUEST_URI is unnecessary since this same check can be performed more efficiently in the RewriteRule directive itself.

You should also ensure that this rule is near the top of your .htaccess file, before any existing rewrites, since we still use REQUEST_URI in the substitution string.

You will need to ensure your browser cache is clear before testing and test first with a 302 (temporary) redirect to avoid potential caching issues.

Upvotes: 1

Related Questions