Last Updated on April 13, 2021 by Amit

A list of all pre-defined mod-rewrite server variables.
Apache mod-rewrite variables
Server variables are used to rewrite/redirect requests based on request headers.
In this article you will find a complete list and explanations with code examples of the server variables provided by Apache mod-rewrite.

Table of contents

%{HTTP_HOST} variable

This variable is used to match against host header. We use it to rewrite/redirect requests based on the http host header.
HTTT_HOST represents your domain name ie : “example.com” or “www.example.com” .

To redirect all www subdomain URLs to a subdirectory named www , we use the following code :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule (.*) /www/$1 [L]

In the code above we check the host header if it matches the required format ( host == www.example.com) the then the rule is triggered. The RewriteRule above rewrites all requests for “www.example.com” domain to the “www” sub directory located in root.

%{REQUEST_URI} variable

We use %{REQUEST_URI} to test the URL path. This variable contains the full request path excluding query string and URL fregment.

If the request is for www.example.com/file.php?q=foo this variable only catches the file path or file name ie: /file.php . This doesn’t catch the query perameters and URL fregments .

We use this variable to rewrite/redirect requests based on requested URL path.

%{HTTPS} variable in mod-rewrite

We use %{HTTPS} variable to check the requested URL scheme. If the request is made using http scheme then this variable returns off otherwise on if the requested URL is using https scheme.

This variable is most commonly using in conditions with RewriteRule to redirect http URLs to https like in the following example :

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com/$1 [R,L]

The rule above checks the URL scheme , if its http ( “HTTPS==off”) then the request is redirected to include https.

%{QUERY_STRING} variable in mod-rewrite

This mod-rewrite server variable is used to check URL queryString.
If the requested URL looks something like example.com/page.php?q=foobar then this variable captures the q=foor query segment from the URL.

We often use this variable in a RewriteCondition to match against URL queryString so that we can redirect/rewrite a request based on that.

In the following example , we check the query string q=foobar&n=1 if it matches the required format and if our RewriteCond matches it then the rule is sent a green signal to redirect the request.

RewriteEngine on

RewriteCond %{QUERY_STRING} ^q=foobar&n=1$
RewriteRule (.*) https://example.com/ [R]

The RewriteRule above redirects any URLs containing the query string ?q=foobar&n=1 to https://example.com .

%{SERVER_NAME} variable in mod-rewrite

This variable is used to test the host header in RewriteCond . %{SERVER_NAME} is same as %{HTTP_HOST} .
But its least used in mod-rewrite.

RewriteEngine on
RewriteCond %{SERVER_NAME} ^www\.example\.com$
RewriteRule (.*) https://example.com/$1 [L]

The code above redirects www subdomain to the main domain.

%{THE_REQUEST} variable in mod-rewrite

This variable represents the full request line sent by a browser to the server.

If a web browser is requesting /file.html then this variable contains the full request string including request method, filename, queryString, fregment and protocol.

Here is how the variable string looks like when /file.html is requested :

GET /file.html HTTP\1.1

We use this in RewriteCond along with RewriteRule to rewrite/redirect the request in safe mode as this variable doesn’t change after execution of RewriteRule. This helps prevents the infinite loop error when you are rewriting a file to the same location with different perameters

The RewriteRule bellow redirects /file.html to /file.html#hello ( the destination file is same but has a different perameter at the end ) .

RewriteEngine on
RewriteCond %{THE_REQUEST} GET\s/file\.html\sHTTP [NC]
RewriteRule ^file.html$ /file.html#hello [NE,R]

If you remove the RewriteCond from the rule above you will get an infinite loop error . THE_REQUST is there just to avoid the loop so that the rule can not match the destination file after execution.

How useful was this post?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 1

No votes so far! Be the first to rate this post.