export .htaccess variable to shell or script - .htaccess

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?

Related

pass a paramter to a script through .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.

mod_rewrite to change "document root" based on query parameter?

I'm trying to figure out how to set up a mod_rewrite rule so that a request for a page, with a URL parameter appended, determines the root from which that file is served.
Here's the setup. In my "foo" directory I have "bar", "bar19", "bar27", etc.
Ideally I'd like to match on the first two characters of the "v" parameter. So like this:
I would like a request for ..................... to be served from:
www.example.com/foo/bar/something.html .......... foo/bar/something.html
www.example.com/foo/bar/something.html?v=19xxx ... foo/bar19/something.html
www.example.com/foo/bar/something.html?v=27xxx ... foo/bar27/something.html
Of course I would expect that if a value for "v" parameter that doesn't have a corresponding directory to 404.
I've done some Mod_Rewrite magic before, but I'm kind of stuck here. Any ideas?
Add a .htaccess file in directory /foo with the following content. Of course you can also insert it into your httpd.conf:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /foo
# match query parameter v with two-digit value; capture
# the value as %2 and the query string before and after v
# as %1 and %3, respectively
RewriteCond %{QUERY_STRING} ^(.*)v=(\d\d)[^&]*(&.*)?$
# match path into "bar" and rest; insert two-digit value from
# RewriteCond inbetween; append rest of query string without v value;
# use case-insensitivity
RewriteRule ^(bar)(.*)$ $1%2$2?%1%3 [NC]
</IfModule>
I think the key is to use captured values from the RewriteCond (accessible as %1, %2, etc.) and at the same time captured values from the RewriteRule itself (as usual $1, $2, etc.).

How the Environment tag E works in .htaccess mod rewrite

I have the following rules in a htaccess file
RewriteEngine On
RewriteRule mytest.php test.php
RewriteCond %{QUERY_STRING} !done
RewriteRule (.*) $1?done [E=TEST:itworks]
The file test.php is simply
<?php
echo "TEST = " . getenv('TEST');
?>
When I enter the request uri test.php, the environment variable TEST is defined and it echoes 'Test = itworks'. However, when I enter the request uri mytest.php, it also goes to test.php, but the environment variable TEST is not defined and it echoes 'Test =' .
Is that the expected behavior? If it is a bug in my environment, never mind. Otherwise, perhaps one could use that simple case to explain to me how it works.
When you go through mytest.php, there is an additional round through mod_rewrite.
The environment variables are then prefixed with REDIRECT_. If you check for REDIRECT_TEST, you will see the desired output
<?php
echo "TEST = " . getenv('TEST') . "/" . getenv('REDIRECT_TEST');
See Available Variables for some details.

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