Kakenx
Kakenx

Reputation: 1445

What does this htaccess code do?

I'm not to versed in htaccess code. Can anyone tell me what this code does? It was messing up PIWIK tracking interface. And not sure if it has also been messing with the tracking code as site visitors have been dropping like flies.

RewriteCond %{REQUEST_URI} !^/download
RewriteCond %{REQUEST_URI} !^/image
RewriteCond %{REQUEST_URI} !^/media
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) /${lc:$1} [R=301,L]

Upvotes: 2

Views: 211

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270609

That rule, in isolation, will take any URI input and force it to all lower-case, then redirect to the lower-cased URI.

http://example.com/Path/To/A-CAPS-URL would be redirected to http://example.com/path/to/a-caps-url

The (.*) captures the entire input URI into $1.

The /${lc:$1} most likely (per docs and convention) sends the captured URI from $1 into an internal function tolower, via RewriteMap.

Here are the relevant Apache docs demonstrating this feature.

However, that RewriteRule alone as you have above (with no other rules in .htaccess) is not likely function correctly. Having no RewriteCond preceding it to match upper-case characters, it would cause an infinite redirection loop.


Updated for full rewrite rules:

Seeing the full set of rewrites, the conditions enforce a redirect to the lower-cased version of any URL which does not begin with /download, /image, /media, as those three conditions are negated with !. The condition matching [A-Z] enforces the redirect only if there is an upper-case letter passed in the input URI (as it would be unnecessary to redirect otherwise).

If this is causing problems with tracking in Piwik, it is likely because the Piwik tracking URI matches one of the conditions and is redirected away from where it can function properly.

Consider adding another condition to prevent Piwik from being rewritten. If the tracker is /piwik.php for example, you could use:

# Don't mess with anything in piwik...
RewriteCond %{REQUEST_URI} !piwik
# Combined the others into one...
RewriteCond %{REQUEST_URI} !^/(download|image|media)
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) /${lc:$1} [R=301,L]

Finally, if you do not have a RewriteMap defined anywhere, the ${lc:$1} is unlikely to be functioning as intended to force lower-case. Make sure your RewriteMap is defined similarly to the examples in the linked documentation.

Upvotes: 3

Related Questions