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:

 

Message Sent

Thank you for registering.

Cardinal Path hosted a live session to connect with you and answer all your questions on Google Analytics.
Get all the expertise and none of the consultancy fees in this not-to-be-missed, rapid-fire virtual event.

Thank you for submitting the form.

Thank you for submitting the form.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you.

Click here to download access the tool.

Message Sent

Thank you for registering.

Message Sent

Thank you.

Message Sent

Thank you.

Message Sent

Thank you

Message Sent

Thank you

Message Sent

Thank you.

Message Sent

Thank you

Message Sent

Thank you.

Message Sent

Success!
Your message was received.

Thank you.

Thank you for registering.

Cardinal Path is continuing with its series of free training. Next we are conducting training on Google Data Studio. Check it out here.

Message Sent

Thank you for registering.

Thank you for your submission.

Your request has been submitted and a rep will reach out to you shortly.

Message Sent

Thank you for your interest.

Thank you for registering.

You should receive a confirmation email from GoToWebinar with your unique webinar login information. If you do not receive this email or have trouble logging in to the event, please email asmaa.mourad@cardinalpath.com.

Thank you for subscribing!

You're now looped into the world's largest GMP resource hub!

Thank you for your submission.

Thank you for your submission.

Thank you for your submission.

Thank you for your submission.

Message Sent

Thank you for registering.

Message Sent

Thank you for your submission.

Thank you for your submission.

Message Sent

Thank you for registering.

Thank you for registering.​

Paid media spend by Government websites increased a whopping 139% YoY in 2020.

2020 Online Behavior Live Dashboard

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

2020 Online Behavior Live Dashboard

Thank you for your submission.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Thank you for registering.

Message Sent

Success! Thank you
for reaching out.