Apache Expression Functions and .htaccess

Soldato
Joined
3 Jun 2005
Posts
3,066
Location
The South
Hi All,

Essentially i'm wanting to Base64 encode the requested file/url and redirect to a script with it as a query string, eg -
Code:
http://localhost/awesome.html --> http://localhost/index.php?q=YXdlc29tZS5odG1s


From reading the documentation (https://httpd.apache.org/docs/2.4/expr.html) this should technically be doable using Apache's Base64 expression function and the usual RewriteRules within htaccess, like so -
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?q=%{base64:$1} [L,QSA]


But i'm getting absolutely no where with expression functions (have tried others like MD5/SHA1 etc) and getting them to run/output anything; expression variables (eg - ${REQUEST_FILENAME}) do work however.

Has anyone had much luck with expression functions within the .htaccess? And/or know where i'm going wrong with the above?

I appreciate this could be handled on the scripts' frontend but at the moment there's a fair few hurdles preventing me doing this hence trying to get the "easier" (oxymoron and a half) option to work.

edit - Just to add, this is using Apache 2.4.41 (supports expressions).
 
Associate
Joined
9 Jun 2004
Posts
423
Disclaimer: I know nothing about Apache (I do embedded development!), but this just piqued my curiosity:
1. It's not clear if can you use ap_expr constructs in a RewriteRule? The docs indicate that you can in a RewriteCond, but it's not mentioned in the RewriteRule stuff.
2. If not, it looks like RewriteMap with an external executable (e.g. just base64 on Linux) to actually do the encoding would work, but it seems a bit heavyweight.
 
Back
Top Bottom