Skip to content

.htaccess - mod_rewrite variables

For everyday work, a handful of variables is enough to find your way around .htaccess rules. Meet the 5 most used ones - it is not much, and they are worth knowing.

This article is about the mod_rewrite module and covers only the most used variables. Their content can be set to anything by the visitor - keep that in mind!

These variables hold, among other things, the client's request and the server settings, which is what we focus on here. They are written in capital letters, and to use one in a rule you wrap it in curly braces preceded by a percent sign, e.g. RewriteCond %{REQUEST_URI}. You can look at their content by adding a set of "debugging" rules to .htaccess.

REQUEST_URI

This variable holds only the path of the request, e.g. for /htaccess-variables-top-5?lang=pl it is "/htaccess-variables-top-5", without the extra query parameters ("?lang=pl").

# Matches every request for an address starting with /htaccess-variables
RewriteCond %{REQUEST_URI} ^/htaccess-variables
RewriteRule .* /redirect-target [R=301,L]

As you can see, this variable gives you much the same thing to compare as a plain RewriteRule. Remember, though, that what RewriteRule tests its regular expression against depends on its context. In an .htaccess file in the site's root folder (e.g. public_html), the value is close to %{REQUEST_URI} - it only lacks the leading slash. But put the same rule in a subfolder (e.g. public_html/images) and check the address /images/cat.webp, and you will find that RewriteRule sees only "cat.webp", while %{REQUEST_URI} still holds the full path, "/images/cat.webp".

QUERY_STRING

This variable holds only the extra query parameters, e.g. in /htaccess-variables-top-5?lang=pl it is "lang=pl" (without the question mark).

# Matches every request for an address with exactly the parameter "lang=pl"
RewriteCond %{QUERY_STRING} ^lang=pl$
RewriteRule .* /redirect-target? [R=301,L]

As you can see, this time a RewriteRule alone will not do, because its value never includes the query parameters. This construction is used a lot, for example to check filters in a shop.

Note the question mark at the end of the target address. Without it, Apache would append the old parameters (here "?lang=pl") to the redirect, which is usually not what you want when redirecting filters. In newer versions of Apache (2.4 and later) the [QSD] flag does the same.

THE_REQUEST

This variable holds the full request line the browser sent to the server: the method, the address and the protocol:

GET /htaccess-variables-top-5 HTTP/1.1

You can use it like this (remember to put the escape character "\" before the space):

# Matches every GET request for /htaccess-variables - the way this article has just been loaded
RewriteCond %{THE_REQUEST} ^GET\ /htaccess-variables
RewriteRule .* /redirect-target [R=301,L]

# If this article had a form, though, sending it with POST would trigger this rule:
RewriteCond %{THE_REQUEST} ^POST\ /htaccess-variable
RewriteRule .* /redirect-target [R=301,L]

As you can see, you get the whole request here, so with slightly more elaborate regular expressions you can reach REQUEST_URI, QUERY_STRING and even the request method.

HTTP_HOST

This variable holds the host name, the part between the protocol (e.g. "http://") and the path (e.g. /index.php). You can use it, for example, to redirect the "www" version to the one "without www":

# If the client asks for "www.bwrite.pl", redirect it to "bwrite.pl"
RewriteCond %{HTTP_HOST} !^bwrite\.pl$
RewriteRule ^ https://bwrite.pl%{REQUEST_URI} [L,R=301]

HTTPS

The %{HTTPS} variable has only two possible values: "on" when the connection uses SSL/TLS, and "off" when it does not. Handy, for example, for redirecting to the https version of a site:

# If the connection is not https, redirect to one that is
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

In the rule above, it may look like %{QUERY_STRING} is missing from the RewriteRule. No need to worry: query parameters are carried over automatically when redirecting.

Other variables

You will find the full list of variables with short examples on AskApache.com. Other mod_rewrite variables also give you access to cookies, the referrer or details of the SSL certificate.

Advertisement