US: (831) 440-7361
India: +919830271197
We Create Websites We Market Websites

HtAccess difficult problems #1

I have spent months working with htaccess doing almost everything possible with it, like checking cookie variable, non-www Domain redirection to www (easy), www subdomain redirection to non-www subdomain (a little tough) e.t.c. The best part of the programming was that we had one htaccess for as many as five sites (plus its alpha, beta sites) and it worked fine with all the regular expressions (everything was variable including the domain name).

Some of difficult problems we faced with htaccess,
Problem 1: Comparison of variables

Solution 1:

According to JDMorgan of webmasterworld.com

There is no ‘native’ support in Apache for comparing two variables, although some operating systems support ‘atomic back-referencess’ which can be used to emulate a compare. This depends on the regex library bundled with the OS> Specifically, POSIX 1003.2 atomic back-references can be used to do a compare by using the fact that if A+A = A+B, then A=B.

RewriteCond %{HTTP_REFERER} ^(http://[^/]+)
RewriteCond %{HTTP_HOST)<>%1 ^([^<]+)<>\1$ [NC]
RewriteRule ^uploads/[^.]+\..{3,4}$ - [L]

Note that the “<>” string is entirely arbitrary and has no special meaning to regular-expressions; It is used here only to demarcate the boundary between the two concatenated variables. The actual ‘compare’ is done in the second RewriteCond, using the atomic back-reference “\1″ to ‘copy’ the value of the string matching the parenthesized pattern directly to its left.

Therefore
if %{HTTP_HOST}<>%{HTTP_REFERER}(partial) == %{HTTP_HOST}<>%{HTTP_HOST}<>%,
then %{HTTP_REFERER}(partial) == %{HTTP_HOST}

This may need some tweaking to fit your actual referrers, since the match between hostname and the partial referrer substring saved in %1 must be exact. And as noted, it will only work on servers which support POSIX 1003.2 regular expressions (FreeBSD is one, and there are others.) I know of no way to support variable-to-variable compares in mod_rewrite without this POSIX 1003.2 trick.

Solution 2:

Set the variable first
SetEnvIfNoCase Referer>http://([a-zA-Z]{2,3})\.idealwebtools\.com\.* HostNameAndReferrerNameAreFromSameDomain=True

And then use it in the logic
RewriteCond %{ENV:HostNameAndReferrerNameAreFromSameDomain} !^True$ [NC]
RewriteRule (.*) redirection [R=301,L]

Saturday, September 16th, 2006 Tech Talks

3 Comments to HtAccess difficult problems #1

  • Billy says:

    Link to more .htaccess files http://www.acomputerportal.com/htaccess.html

    Link to more Apache open-source software and Apache Servers. Mod Rewrite. http://www.acomputerportal.com/apache.html

  • AjiNIMC says:

    Thanks Billy, for the collection. You can also the check the log file configuration post.

  • Leave a Reply

    Get a Custom Quote & Free Website Review (Report includes 200+ keywords, ranking & more - worth $200)