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}
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
.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With