21

I'm trying to a build a .htaccess file with some rewrite rules and would like to know what several variables actually contain when my request is handled. Is there anyway of seeing what their values would be when Apache handles the request?

E.g. print the contents of %{HTTP_USER_AGENT}

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Steve Whitfield
  • 1,981
  • 3
  • 21
  • 30

3 Answers3

26

Sure. Create this sort of .php file (echo.php):

<?php
phpinfo(INFO_VARIABLES);
?>

Add this rule in .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_URI} !echo.php
RewriteRule .* echo.php?ua=%{HTTP_USER_AGENT}&https=%{HTTPS} [L]

Add more parameters if necessary.

Now call any URL and check the output (the GET parameters should be on the top of table).

But, TBH, almost all of this info is received by Apache and is available to PHP anyway: look at $_SERVER.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • Thanks - I know you can get all that stuff from PHP. I just wanted to see what the contents of a few variables were (HTTP_USER_AGENT not being one of them actually) and it didn't occur to me to do what you did there. :) – Steve Whitfield Jul 28 '11 at 15:59
  • 1
    The question is "Is it possible to output the environment variables of the .htaccess?" I guess the correct answer would be how to log the value of the variables in the apache logs and not how to output it in PHP. Furthermore, this is assuming that the current apache server configuration is able to serve PHP script – maxwell2022 Jul 20 '13 at 02:30
  • Why is there an exclamation point, aka `!echo.php`? Isn't that negation? – felwithe May 23 '18 at 14:27
  • 1
    @felwithe Your observation is correct. I do not remember all the details now (as I have not been using Apache/mod_rewrite for almost 5 years now) .. but from what I remember it's to prevent endless rewrite loop (a `[L]` flag does not fully stop rewrite process -- only current iteration/step -- at very least back then -- see https://stackoverflow.com/a/6800150/783119). – LazyOne May 23 '18 at 14:34
10

Yes, it is possible to output the request variables through .htaccess.

You can do it by "hacking" a custom error status message. As long as you have AllowOverride set to FileInfo you can set and trigger a custom error response in your .htaccess file with the desired variables in the output:

ErrorDocument 404 "Request: %{THE_REQUEST} Referrer: %{HTTP_REFERER} Host: %{HTTP_HOST}"
RewriteRule ^ - [L,R=404]

For reference, a list of available variables is available in the Apache documentation.

Interestingly, you are not limited to using 400 or 500 error statuses for the error response override. You can even override the status 200 "ErrorDocument." That means you can do a lot more than just output variables for troubleshooting with this trick. Throw an <If> tag around it and you have a document!

<If "%{REQUEST_URI} =~ /compliance.html$/">
    ErrorDocument 200 "<html><body><h1>Yes, Max. Those were geeks.</h1></body>"
    RewriteRule ^ - [L,R=200]   
</If>
PeterA
  • 775
  • 9
  • 15
1

Another way of achieving this without altering the behavior of your routes (no 404 or redirect) is to send the desired value as a response header.

RewriteRule .* - [ENV=REQUEST_URI:%{REQUEST_URI}]
Header set x-request-uri %{REQUEST_URI}e

Then you can see the output with curl -I

$ curl -I http://localhost:8095/api/my/request/uri
...
x-request-uri: /api/my/request/uri

Your route will still go to where it was supposed to go, but you get a response header with the value you wanted

santiago arizti
  • 4,175
  • 3
  • 37
  • 50
  • Unfortunately, this probably seems to work because REQUEST_URI does not change; if I try arbitrary variable `RewriteRule .* - [ENV=MYVAR:AAAA]`, then `Header set x-request-uri %{MYVAR}e` for me just prints "(null)", probably because of (https://stackoverflow.com/q/19362746) "The SetEnv directive runs late during request processing meaning that directives such as SetEnvIf and RewriteCond will not see the variables set with it." [Setting Environment Variables](http://httpd.apache.org/docs/current/env.html#setting) – sdbbs Oct 18 '21 at 09:52