Shaun Mccran

My digital playground

14
J
U
N
2011

Using url rewriting ( .htaccess or httpd.ini ) to block hot linking resources

After my recent move to HostMediaUK I've been able to see more in depth statistics about one of my sites, including traffic and data usage. This also includes having visibility of other domains that are linking directly to my content. This is popularly known as hot linking, and if you haven't asked permission is considered very impolite.

This also uses up your servers bandwidth rather than theirs. This article explores how I use a URL access file, either .htaccess of http.ini depending on your platform, to stop other domains from linking directly to your hosted resources.

[ More ]

21
A
P
R
2011

Using URL re-writing to provide friendly 404 error screens

A while ago I wrote a blog entry about changing the 404, page not found, handler in IIS ( http://www.mccran.co.uk/index.cfm/2010/3/2/Changing-the-404-template-handler-in-IIS). More recently I've moved some hosting to a Linux based server, and I didn't have the same server side options available to me.

The problem I've had setting up a global 404 handler is that I can setup a front end friendly error page easily enough, but the method used to set it up hugely affects my ability to actually report what the error was.

For example in the Linux admin area I can specify a path to a 404 template, but this appears to actually relocate the user to the file specified, leaving any error and its associated information behind.

Similarly the 'onMissingTemplate' Application.cfc function only fires when a missing ColdFusion template is requested. Not just any old url that someone tries on my site.

What I eventually ended out with is a URL rewrite rule that catches any page request that does not match an already defined re-write rule.

So if a user asks for 'www.mysite.com/contact/' the following rule would be found and used:

view plain print about
1RewriteRule ^/contact/$ index.cfm?go=contact

But if they asked for any non matched rule, for example 'www.mysite.com/contactx/', or 'www.mysite.com/hack-attempt/' then the following rules would kick in and divert the user:

view plain print about
1# Error handlers
2RewriteRule ^([^/.]+)/$ index.cfm?go=error&error=$1
3RewriteRule ^([^/.]+)$ index.cfm?go=error&error=$1

The rule above just sends a user to my error page. It also appends the string matched in the Regular Expression to the end of the URL (error=$1).

This is so I can pick it up in a ColdFusion variable scope and log it, to actually let me know what the error was.

There is another documented way of using URL re-writing to redirect users to custom error pages. You can use custom error pages for any error type as long as you know its number by adding the following to your re-write file:

view plain print about
1ErrorDocument errorNumber path/file.htm

As an example if I had the file 'error.htm' in the root directory of my site and I wanted to use it for a 404 error I would use:

view plain print about
1ErrorDocument 404 /error.htm

Some of the more common server error responses are:

view plain print about
1401 - Authorization Required
2400 - Bad request
3403 - Forbidden
4404 - Wrong page
5500 - Internal Server Error

10
M
A
Y
2010

The SEO way to safely redirect a Page or folder using 301 redirects

In a recent project we are restructuring a site to be more intuitively architected. But what impact will moving the directories or pages have on all that hard earned Search Engine Ranking Optimisation work?

This article deals with how to safely redirect users from an old page or folder to the new URL, and keep Search Engine's informed of your changes without leading them to dead content (Dead content is bad and will adversely affect your Site rankings).

[ More ]

28
J
A
N
2010

Passing url variables through Isapi re write - Regular Expression

One of the more common tasks in ColdFusion development is passing variables through the URL string. We are all familiar with the idea that the question mark (?) denotes the url query string start, and that name value pairs are separated with the ampersand (&).

I usually avoid using this in display templates, as it isn't great exposing your internal site workings to customers, and with Fusebox it is very easy to pass the URL variables to an "act_" template and remain hidden.

What happens when you want to use dynamic url variables with a URL re writing application like Isapi re write? I've been using Isapi re write in some FuseBox framework application, and it is relatively easy to set up a rewrite rule, as shown below.

view plain print about
1RewriteRule home(/) index.cfm?fuseaction=circuitname.circuitfunction

Where the url /home/ would actually serve up the content specified in the fuse specified. But this is hard coded. What about dynamic variables?

We can create a regular expression to handle the translation of the variables.

view plain print about
1RewriteRule destination/(.*)/(.*)/ index.cfm?go=circuitname.circuitfunction&$1=$2

We use a similar URL, but append the dollar ($) 1 = dollar ($) 2 string. In the re write rule we specify that appended variables are transposed into the string using the slash (/) as a separator.

So as an example we could pass a product id of 24 into the rule like this:

view plain print about
1www.siteurl.com/cart/productId/24/
2RewriteRule buy/(.*)/(.*)/ index.cfm?go=cart.buy&$1=$2

It would be rewritten to the more familiar url string. A handy way of continuing to mask the url.