pass a paramter to a script through .htaccess - .htaccess

I am able to execute script through .htaccess but I would like to pass a parameter to this script Is there a way to pass input parameter to a script in .htaccess?
I am not able to set environment variable and use it inside the script.
Is it possible to use environment variable defined in .htaccess in a script?
RewriteRule ^(.*)$ new.sh [E=VAR:$1,L]
I would like to pass VAR parameter to new.sh script
to be executed like new.sh VAR

You can either pass it as a query string.
RewriteRule ^(.*)$ new.sh?VAR=$1 [E=VAR:$1,L]
Or, since it's being set as an environment variable, read the current environment variables from inside of your script; i.e., VAR has been set as an environment variable in your example.

Related

export .htaccess variable to shell or script

In .htaccess file, I have this line RewriteRule ^(.*)$ script.sh?sub=$1
Is there anyway to show the value of $1 in the shell
command like echo $sub
or can I pass $1 as a parameter to script.sh?
You can set an environment variable - is that readable from your script? For example, to set an environment variable called MY_VARIABLE to the value of the $1 backreference:
RewriteRule ^(.*)$ script.sh?sub=$1 [E=MY_VARIABLE:$1,L]
I've also added the L (last) flag just in case. Maybe you can remove the ?sub=$1 part if it's not being used?

Where to set PHP environmental variables? [duplicate]

I am trying to set an environment variable in an .htaccess file and retrieve it in PHP. I've looked through a bunch of other threads here on SO but everything I've tried so far has failed.
I've added this line to the .htaccess file:
SetEnv SPECIAL_PATH /foo/bin
I have tried retrieving this value using the getenv() PHP function:
<?php $specialPath = getenv('SPECIAL_PATH'); ?>
I have runned phpinfo() to see the list of available environment variables, SPECIAL_PATH is not there. I am puzzled as to why this is not working.
Thank you!
Assuming your configuration has AllowOverrides with .htaccess, you must enable mod_env in Apache for this to work.
Apache - mod_env
Apache Docs custom-error.html covers environment variables passed to error handling pages
Says "REDIRECT_ environment variables are created from the environment variables which existed prior to the redirect. They are renamed with a REDIRECT_ prefix, i.e., HTTP_USER_AGENT becomes REDIRECT_HTTP_USER_AGENT."
Says "None of these will be set if the ErrorDocument target is an external redirect (anything starting with a scheme name like http:, even if it refers to the same host as the server)."
Says about SetEnv: "The internal environment variables set by this directive are set after most early request processing directives are run, such as access control and URI-to-filename mapping. If the environment variable you're setting is meant as input into this early phase of processing such as the RewriteRule directive, you should instead set the environment variable with SetEnvIf."
On some servers, user-declared environment variables must start with 'HTTP_' for security purposes, e.g.: SetEnv HTTP_MY_VARIABLE "my value"
Here are some .htaccess ways of setting and using server environment variables, taken from my modifying the Perishable Press 5G Blacklist/Firewall http://perishablepress.com/5g-blacklist-2013/ to use environment variable reporting:
SetEnv myServerName %{SERVER_NAME}
RewriteCond %{QUERY_STRING} (base64_encode|localhost|mosconfig|open_basedir) [NC,OR]
RewriteCond %{QUERY_STRING} (boot\.ini|echo.*kae|etc/passwd) [NC,OR]
RewriteCond %{QUERY_STRING} (GLOBALS|REQUEST)(=|\[|%) [NC]
RewriteRule .* - [E=badQueryString:%0--%1--%2,F,L]
SetEnvIfNoCase User-Agent ^$ noUserAgent
SetEnvIfNoCase User-Agent (binlar|casper|cmsworldmap|comodo|diavol|dotbot|feedfinder|flicky|ia_archiver|jakarta|kmccrew|nutch|planetwork|purebot|pycurl|skygrid|sucker|turnit|vikspider|zmeu) badUserAgent=$1
<limit GET POST PUT>
Order Allow,Deny
Allow from all
Deny from env=badUserAgent
</limit>
Notice the use of paramters, e.g. $0--%1--%2. %0 gives the full string, %1 gives the match from the 1st parenthesized statement, %2 the 2nd. The hyphens are literal display characters, to visually separate parameter results (don't think is any way to put spaces in there).
Here are some PHP methods of accessing the environment variables (in my case from 403.php and 404.php).
Note that you don't look in phpinfo(), but in $SERVER, and that your variables get prefixed with REDIRECT
Note also that with a 403/404 redirect, the QUERY_STRING becomes REDIRECT_QUERY_STRING
This is stuff that could easily be server dependent, so check $_SERVER for your actual values. For example,
if (getenv("HTTP_REFERER") !== FALSE) {
$httpref = getenv("HTTP_REFERER");
} else {
$httpref = '';
}
if (isset($_SERVER['REDIRECT_STATUS'])) {
$status = $_SERVER['REDIRECT_STATUS'];
} else {
$status = '';
}
if (isset($_SERVER['REMOTE_HOST'])) {
$remoteHost = $_SERVER['REMOTE_HOST'];
} else {
$remoteHost = '';
}
if (isset($_SERVER['REDIRECT_QUERY_STRING'])) {
$querystring = $_SERVER['REDIRECT_QUERY_STRING'];
} else {
$querystring = '';
}
if (isset($_SERVER['REDIRECT_noUserAgent']) ) {
$htaccessErrors[] = 'NoUserAgent';
}
if (getenv("REDIRECT_badQueryString") !== FALSE) {
/* must exactly match what shows up in $_SERVER, is case sensitive (e.g. badQueryString not BadQueryString) */
$htaccessErrors[] = 'badQueryString:'.getenv("REDIRECT_badQueryString");
}
I cover in some depth in http://lcblog.lernerconsult.com/2013-server-alert-you-file-not-found-errors/

.htaccess how to create an URLs like http://example.com/~someuser

How do I can create and process an URLs like this via .htaccess rules: http://example.com/~someuser
And then get a parameter "username" as a part of $_GET array. Thanks in advance.
Something like this:
RewriteEngine On
RewriteRule ^~([^/]+)/?$ /script.php?user=$1 [L]
If those rules are placed in the htaccess file in your document root, then it'll capture the "someuser" part of the URL and rewrite it to the script.php script and set it as the $_GET['user'] variable. You can replace script.php with whatever name of the script you want the variable to go to.

setting and using owne variable in .htaccess

I set variable in .htaccess like this
SetEnv clubhub_path clubhub-ui
And I can acces it in php with
$_SERVER['clubhub_path];
But I wanted to use it in .htaccess in Rewrite Cond
RewriteCond %{REQUEST_URI} ^(?!/%{SERVER:clubhub_path}/media/).+ [NC]
I found somewhere this(%{SERVER:clubhub_path}) should work but it doesn't. I tried all the combinations with {}, %, $. No success...
Cheers
SetEnv sets environment variables, so it should be available in %{ENV:clubhub_path} but i've tried that and doesn't seem to be working.
However since you can set environment variables with RewriteRules you could write it like this:
RewriteRule ^ - [E=clubhub_path:clubhub-ui]
This rule matches every url, but doesn't rewrite it to anyting just sets an environment variable that will be available in RewriteConds like:
RewriteCond %{ENV:clubhub_path} ^clubhub-ui$
This should look like exactly the same once it gets to php. (in $_SERVER superglobal, or getenv())
Update
However you still can't use your %{ENV:variable} in the right hand side of RewriteCond. I think the only way to do this if you are on apache 2.4 and you can use ap_expr
RewriteCond expr "! '%{REQUEST_URI}' -strcmatch '/%{ENV:clubhub_path}/media/*'"
In my test environment this seem to be doing what you want (negative match on the '/clubhub-ui/media' urls, but apache2.4 is still pretty new, you probably can't have it installed if you are not self-hosted.
Maybe RewriteMap with a prg parameter and a script could implement your idea.

Pass variables from htaccess to bash script

I'm trying to pass the value of a cookie to a bash script:
RewriteCond %{HTTP_COOKIE} mycookie=(.*) [NC]
RewriteRule .* script.sh?cookievar=%1
... but can't seem to find out how to read the GET variable in the bash script. (I suppose I'm asking Google the wrong queries, but can't find any info on this).
Is this even possible, and if so, please how?
Thanks, David
You have to look at QUERY_STRING environment variable in Bash in order to access GET variables. In your case it should be set to cookievar=VALUE. To extract a variable's value, use something like this:
COOKIEVAR=$(echo ${QUERY_STRING} | sed -n -e 's/^.*cookievar=\([^&]*\).*$/\1/p' -e 's/%20/ /g')
Good luck!

Resources