Latest Tweets404 Fix: Block Nuisance Requests for Non-Existent Files: perishablepress.com/block-nuis…
Perishable Press

Case-Insensitive RedirectMatch

Cool trick that you may not have known about.. it’s possible to get case-insensitive matching with the powerful RedirectMatch directive. Normally, you would just write your redirect as something like this:

RedirectMatch 301 /phpMyAdmin http://example.com/somewhere/

This works great, but it’s case-sensitive. You could just match the all-lowercase version, but there are some phrases — such as “phpMyAdmin” — that really benefit from going the case-insensitive route. Those familiar with Apache might be screaming, “just use a rewrite rule!” Something like this will certainly get you there:

<IfModule mod_rewrite.c>
 RewriteCond %{REQUEST_URI} /phpMyAdmin [NC]
 RewriteRule .* http://example.com/somewhere-else/ [R=301,L] 
</IfModule>

Notice the [NC] flag? That tells Apache to ignore casing for the pattern match. This works great, but there are situations where you would rather just keep it simple with good ‘ol RedirectMatch. When? Let me give you an example with the recent WordPress add-on for the 5G Blacklist, which originally looked like this:

# 5G WP
RedirectMatch 403 /\$\&
RedirectMatch 403 /\.(bash|git|hg|log|svn|swp|tar)
RedirectMatch 403 /(1|contact|i|index1|iprober|phpinfo|phpspy|product|signup|t|test|timthumb|tz|visit|webshell|wp-signup).php
RedirectMatch 403 /(author-panel|class|database|manage|phpMyAdmin|register|submit-articles|system|usage|webmaster)/?$
RedirectMatch 403 /(=|_mm|cgi|cvs|dbscripts|jsp|rnd|userfiles)

Simple and effective, made super lightweight and awesome mainly because of the flexible RedirectMatch directive. But notice the “phpMyAdmin” in the penultimate directive — as Andy W reminds us:

Your WP blacklist checks for “phpMyAdmin”. As I understand it RedirectMatch is case sensitive so it wouldn’t block “phpmyadmin” (all lowercase) which I recollect seeing on old logs for my site.

Pattern-matching with case-insensitivity increases the scope of your .htaccess redirect rules. For the RedirectMatch directive, here’s how to do it..

Case-Insensitive RedirectMatch

Fortunately, Apache makes it easy to declare case-insensitivity with RedirectMatch. Simply precede the pattern with “(?i)” (without the quotes). Returning to our initial example, we can get case-insensitivity like so:

RedirectMatch 301 (?i)/phpMyAdmin http://example.com/somewhere-else/

That’s all you need to match all the crazy variations for requests such as phpMyAdmin:

  • phpMYadmin
  • PHPmyAdmin
  • phpmyadmin
  • PHPMYADMIN
  • PHPMyAdmin
  • phpMyAdmin

And here is the 5G WP add-on, now with case-insensitivity:

RedirectMatch 403 /\$\&
RedirectMatch 403 (?i)/\.(bash|git|hg|log|svn|swp|tar)
RedirectMatch 403 (?i)/(1|contact|i|index1|iprober|phpinfo|phpspy|product|signup|t|test|timthumb|tz|visit|webshell|wp-signup).php
RedirectMatch 403 (?i)/(author-panel|class|database|manage|phpMyAdmin|register|submit-articles|system|usage|webmaster)/?$
RedirectMatch 403 (?i)/(=|_mm|cgi|cvs|dbscripts|jsp|rnd|userfiles)

When it comes to redirecting most requests, its all lowercase anyway. Or you can use RewriteRule to establish case-insensitivity. But for some situations, it’s good to know that you can also roll with RedirectMatch by simply adding the (?i) to the rule.

Jeff Starr
About the Author Jeff Starr = Web Developer. Book Author. Secretly Important.
Archives
9 responses
  1. And presmably the ‘(?i)’ could be added as prefixes to the main 5G RedirectMatch 403 strings too?

  2. Hi Jeff,

    Another great article. I’m still (avoiding) learning about regular expressions. I didn’t know about “(?i)” so this post has proved very useful to me.

    P.S. Thanks for the mention!

  3. Bharat Mandava April 29, 2012 @ 11:27 am

    Thanks for this great article, learning some stuff not related to my niche.

  4. Andreas Kohl June 19, 2012 @ 7:20 am

    Hi,

    Could you tell me what’s the code if have more sites on same server?

    Example:

    http://www.site1.com/phpmyAdmin
    http://www.site2.com/phpMyAdmin
    http://www.site3.com/phpmyadmin

    And I wish to put the command
    RedirectMatch 301 (?i)/phpMyAdmin http://www.site1.com/
    RedirectMatch 301 (?i)/phpMyAdmin http://www.site2.com/
    RedirectMatch 301 (?i)/phpMyAdmin http://www.site3.com/

    I’ve tried to put it on httpd.conf but than all redirects goes to http://www.site1.com/.

    Thank you.

    PS: If I put in the .htaccess it doesn’t work, ’cause the “phpmyadmin” is loaded from other directory (is not on the site directory).

  5. Thanks for this. I noticed some interesting behavior .. if you try this:

    RedirectMatch (?i)/community http://www.example.com

    It will work .. but it will ALSO redirect patterns with a hyphen for example:

    http://www.site.com/community-ties

    :(

    • Jeff Starr

      Correct, that is because the pattern you have set up is non-terminating, so it will match any request that begins with /community. To match only /community, try this:

      RedirectMatch (?i)/community/?$ http://www.example.com

      That will match either /community or /community/ in case-insensitive fashion and won’t match anything else :)

  6. Thanks. Just what I needed. Now it works.

[ Comments are closed for this post ]