Last week I posted some simple htaccess rewrites. This week I want to take it a step further and look at the RewriteCond property, along with three neat rewrites you can use.
As its name suggests, RewriteCond allows you to set conditions for a rewrite, including filtering by referer, saved information (in cookies), server information, and more. Each condition is applied to each rewrite rule, from the conditions line downwards, until it hits a rule marked as the “last rule” with an [L] tag. (For more tags see: Regex Character Definitions for htaccess) Following rules will be executed as well, but not with the condition applied.
The available conditions are:
|connection & request:|
There are lots of uses for this, but a few of my favourites are:
HTTP_HOST & SERVER_NAME
These are incredibly similar. HTTP_HOST is retrieved from the HTTP Request header. It defines the host of the resource being used. SERVER_NAME is defined by the server config. So SERVER_NAME is more reliable, but is dependent on the server.
I mentioned, in my last post, that you can use it as follows:
This will forward requests for the non-www version of your site to the www version of your site. Nice for uniformity and for SEO.
This allows you to define rules based on the IP address of the user. Find a particular user, or set of users, from a specific host are visiting you a lot? Want to leave them a message? Create an alternative landing page for them, then write the following into your htaccess file:
This is also really useful if you have test pages that you don’t want outside users to see. For example:
This condition/rule combo checks the IP address of the visitor and, if it does not match the pattern put in, redirects all attempts to access it.
Have a lot of traffic coming from a specific site that’ going to the wrong page? Like, say, do you have each of Google’s Certified Partner pages linking to your Google Analytics page, instead of their respective services?
So in the case of Google linking to our GA page instead of our Urchin page from their Urchin Certified Partner Page: