In most cases, you’re likely going to need to test for a variety of conditions when writing rules within the .htaccess file.
To test Rewrite Conditions and process Rewrite Rules, you’ll need to be quite familiar with server variables and their values.
There are a multitude of server variables available to test within conditions and apply within rules.
I’m only listing the most common server variables you’re likely to encounter, although there is a long list and cheat sheet you can review at AskApache.com.
For an example of how to use server variables with writing conditions and rules, review how to redirect both non www and www web traffic to a specific type of domain.
Below are the variables I tend to use when writing htaccess rules:
- HTTP_USER_AGENT – Use this to determine what type of device or browser is making the request
- HTTP_REFERER – where / who request is coming from
- HTTP_HOST – returns the hostname of server, typically the domain name for the website where the .htaccess is hosted
- REMOTE_ADDR – IP Address of the device making request
- REMOTE_HOST – IP Address of the host making request
- REQUEST_METHOD – type of request method made (GET, POST, DELETE, etc.)
- SCRIPT_FILENAME – the fullpath of filename
- PATH_INFO – server path
- QUERY_STRING – the URL query string of request
- AUTH_TYPE – type of authentication request made
Dates and Times
- THE_REQUEST – the type of request made with URL path
- REQUEST_URI – the fullpath of filename
- REQUEST_FILENAME – the fullpath of request filename from web root directory