Sometimes the rules you add to .htaccess still refuse to work, even though you checked the syntax, got rid of the typos and put a backslash ("\") before every special character. What now? Time for some debugging, of course!
Looking at the values of variables
You might say the word "debug" does not quite fit here, but I decided to use it anyway - not exactly the thing, but close enough 😉
As everyone knows, the most powerful debugging tool, the crème de la crème of its very being, is... the "print" function (or, in PHP, also "echo", "print_r", "var_dump" - but let's get back to Apache). So the natural question is: can you check the values of the variables you use in .htaccess? Yes, and there are a few ways to do it.
Using HTTP headers
Personally, this is my favourite method: the simplest and quickest, and enough in most cases. I have prepared the set of rules below, so it is basically a ready-made solution, just right for quick debugging:
RewriteEngine On
RewriteRule (.) - [E=DUCH512_REQUEST:$1]
RewriteRule .* - [E=DUCH512_HTTPS:%{HTTPS}]
RewriteRule .* - [E=DUCH512_HTTP_HOST:%{HTTP_HOST}]
RewriteRule .* - [E=DUCH512_QUERY_STRING:%{QUERY_STRING}]
RewriteRule .* - [E=DUCH512_THE_REQUEST:%{THE_REQUEST}]
RewriteRule .* - [E=DUCH512_REQUEST_URI:%{REQUEST_URI}]
Header set X-Duch512-Request "%{DUCH512_REQUEST}e"
Header set X-Duch512-Https "%{DUCH512_HTTPS}e"
Header set X-Duch512-Http-Host "%{DUCH512_HTTP_HOST}e"
Header set X-Duch512-Query-String "%{DUCH512_QUERY_STRING}e"
Header set X-Duch512-The-Request "%{DUCH512_THE_REQUEST}e"
Header set X-Duch512-Request-Uri "%{DUCH512_REQUEST_URI}e"
Header set X-Duch512-R-Request "%{REDIRECT_DUCH512_REQUEST}e"
Header set X-Duch512-R-Https "%{REDIRECT_DUCH512_HTTPS}e"
Header set X-Duch512-R-Http-Host "%{REDIRECT_DUCH512_HTTP_HOST}e"
Header set X-Duch512-R-Query-String "%{REDIRECT_DUCH512_QUERY_STRING}e"
Header set X-Duch512-R-The-Request "%{REDIRECT_DUCH512_THE_REQUEST}e"
Header set X-Duch512-R-Request-Uri "%{REDIRECT_DUCH512_REQUEST_URI}e"
The set includes the 5 most used variables of Apache plus the request as seen by a "plain" RewriteRule (handy when creating 301 redirects or noindex rules). You can extend or change the set in the same way to suit your needs.
The principle is simple:
- With "RewriteRule" and the "E" flag we copy the content of the variables into environment variables,
- With "Header set" we set the values of our own headers, which we will see in the response.
Now when you open any page, in the developer tools, on the "Network" tab, after selecting the right request you will find our headers among the response headers:

Now you know where you stand, and you will spot a missing slash or a stray space much faster. You can also use browser extensions that show the headers, for example in the toolbar.
The "X-" prefix, which by convention marks custom headers, plus our own "DUCH512" name keep them nicely grouped (most header viewers sort them alphabetically) and avoid clashes with existing headers.
Starting custom headers with "X-" is a convention that is widely used. It is worth knowing, though, that for wider use (e.g. when building applications that rely on headers) it is no longer recommended, see RFC 6648.
Remove these rules when you are done. Every visitor can see the headers, and the variables can reveal more about the server's configuration than you would like to show. While testing, you can also limit them to your own IP address.
Using PHP + .htaccess
This approach is more complicated and not always usable, e.g. when you are fixing something "in production", that is on a shop's site that customers are visiting at the same time.
It works by swapping the response of error pages (e.g. 404) or loading a .php file instead of the requested URL - which is rather not what we want. So if you are really interested, see this question on Stack Overflow, if only because the answers show other interesting things Apache can do 😉