Technology Services

Rewrititsu: The Gentlemanly Art of .htaccess Rewrites Pt. 2

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:

HTTP headers:
HTTP_USER_AGENT
HTTP_REFERER
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_ACCEPT
HTTP_USER_AGENT
HTTP_REFERER
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_ACCEPT
server internals:
DOCUMENT_ROOT
SERVER_ADMIN
SERVER_NAME
SERVER_ADDR
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE
connection & request:
REMOTE_ADDR
REMOTE_HOST
REMOTE_PORT
REMOTE_USER
REMOTE_IDENT
REQUEST_METHOD
SCRIPT_FILENAME
PATH_INFO
QUERY_STRING
AUTH_TYPE
system stuff:
TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME
specials:
API_VERSION
THE_REQUEST
REQUEST_URI
REQUEST_FILENAME
IS_SUBREQ
HTTPS

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.

Uses:

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.

REMOTE_ADDR

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.

HTTP_REFERER

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:

 

Cardinal Path

Share
Published by
Cardinal Path

Recent Posts

Optimizing user experiences with Digital Experience Analytics (DXA) platforms

As consumers become increasingly digitally savvy, and more and more brand touchpoints take place online,…

1 month ago

Enabling Value-Based Bidding with Google Tightlock

Marketers are on a constant journey to optimize the efficiency of paid search advertising. In…

2 months ago

Resolving “Unassigned” Traffic in GA4

Unassigned traffic in Google Analytics 4 (GA4) can be frustrating for data analysts to deal…

2 months ago

This website uses cookies.