is it possible to have in .htaccess (Apache 2.2) a url condition that when met it executes the RequestHeader directive? Something like:
if ( %{HTTP_HOST} == "example.com" ){
RequestHeader unset Set-Cookie
}
You can use mod_setenvif directive for this:
SetEnvIf Host ^(www\.)?example\.com$ NO_COOKIE
RequestHeader unset Set-Cookie env=NO_COOKIE
Related
I've an apache 2.4 on a redhat server and I create an http-header in the httpd.conf with this directive:
RewriteEngine on
RewriteCond %{REMOTE_USER} (.*)
RewriteRule .* - [E=X_REMOTE_USER:%1]
RequestHeader set SM_USER %{X_REMOTE_USER}e
the SM_USER header is correctly created but is with domain (utente#domain.com).
I would like to remove the #domain.com using a sort of substring (even creating another header). which syntax I should to use?
thanks
I've found the solution using the regular expression:
RequestHeader set SM_USER %{X_REMOTE_USER}e
RequestHeader edit SM_USER (?=#)(\S+)
I'm trying to set HTTP header X-Robots-Tag to "noindex" on any URL starting with /exit/, but i can't figure out why SetEnvIf Request_URI simply does not match my URLs.
Part of my .htaccess file where i'm trying to solve the issue:
# For info
SetEnvIf Request_URI ^(.*)$ REQUEST_URI=$1
Header set X-Request-Uri "%{REQUEST_URI}e"
# Works
SetEnvIf Request_URI ^/.* WHY1
Header set X-Test1 "test1" env=WHY1
# Does not work any more
SetEnvIf Request_URI ^/e.* WHY2
Header set X-Test2 "test2" env=WHY2
SetEnvIf Request_URI ^\/e.* WHY3
Header set X-Test3 "test3" env=WHY3
SetEnvIf Request_URI ^\/\e.* WHY4
Header set X-Test4 "test4" env=WHY4
SetEnvIf Request_URI ^/\e.* WHY5
Header set X-Test5 "test5" env=WHY5
# What i'd like it worked
SetEnvIf Request_URI ^/exit/.* NOINDEX
Header set X-Robots-Tag "noindex" env=NOINDEX
This is what i get in network tab in Chrome browser:
x-request-uri: /exit/22216832/
x-test1: test1
This is what i actually expected:
x-request-uri: /exit/22216832/
x-test1: test1
x-test2: test2
x-test3: test3
x-test4: test4
x-test5: test5
x-robots-tag: noindex
EDIT
I sacrificed a little more of my hair and found out that i was actually overwriting Request_URI which seems to be some kind of global variable. After more testing i also figured out that this variable is a little liar:
.htaccess file:
# Getting value from a global variable
Header set X-Request-Uri "%{Request_URI}e"
# Expecting that Request_URI1 will have the same value as Request_URI
SetEnvIf Request_URI ^(.*)$ Request_URI1=$1
Header set X-Request-Uri1 "%{Request_URI1}e"
Network tab in Chrome browser:
x-request-uri: /exit/12313/
x-request-uri1: /index.php
So Request_URI in the global space is /exit/12313/, but in case of SetEnvIf it is /index.php and they say magic does not exist ☺.
I have the following url -
http://ip:8080/dashboard/db/testdash?id=add&t1=abc
I want to strip off t1 from the url and add that as a header to my proxy request. I have working reverse proxy conf and I want to intercept this url strip the t1 parameter and add that as header which will be sent to the destination
I have the following configuration for my virtual host listening on 8080
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)t1=(.*)
>>>Need to add the rewrite logic here
>>>Add rewrite conf here change url to /dashboard/db/testdash?id=add
//Add the request header
>>>RequestHeader set X-T1 "value of the query parameter"
//Proxy
ProxyRequests Off
ProxyPass "/" http://localhost:3000/
ProxyPassReverse "/" http://localhost:3000/
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "*"
# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
I am completely new to httpd so don't how to do this!
remove ProxyPass since we want to use the variable
add RewriteRule (^/.*) http://localhost:3000/$1?%1
Note, this matching is not sufficient if the URL can ever have more keys at the end! A more robust regex would only gobble up the t1 value by capturing up to &. e.g. ^(.*)t1=([^&].*)(.*) then substitute %1%3
Add a quirky empty section to make up for the removed ProxyPass
<Proxy http://localhost:3000/ >
</Proxy>
I am trying to add an Access-Control-Origin header in .htaccess for all URIs ending with .json. I cannot use <FilesMatch> as my paths are rewritten by mod_rewrite. (Or if I can it doesn't work.)
I found on Stack that it should be possible to do it with an env variable:
<IfModule mod_headers.c>
SetEnvIf Request_URI "\.json$" IS_JSON=TRUE
# ".json$" yields the same result
Header set Access-Control-Allow-Origin "*" env=IS_JSON
# "Header add" yields the same result
</IfModule>
But it does never add the header.
I tried using a snipper from another stack answer to check if the env variable is there using:
RewriteCond %{REQUEST_URI} !echo.php
RewriteRule .* echo.php?uri=%{REQUEST_URI}&json=%{ENV:IS_JSON} [L]
And it really was true.
When I remove the condition and leave it as:
Header set Access-Control-Allow-Origin "*"
the header is added, so the mod is loaded, but I would like to limit it for ".json$" only.
What am I doing wrong?
Instead of Header you probably meant to use RequestHeader to send custom headers to your code
SetEnvIf Request_URI \.json$ IS_JSON
RequestHeader set Access-Control-Allow-Origin "*" env=IS_JSON
Cert-based authentication in OpenAM need to set http header X-Client-Cert. I want use apache as reverse proxy and to set this header, when url is /openam/UI/Login?module=PKI.
/openam/UI/Login is for username and password authentication.
I have this configuration:
...
ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/
RequestHeader set X-Client-Cert ""
<Location "/openam/UI/Login/PKI">
RequestHeader set X-Client-Cert "%{SSL_CLIENT_CERT}s"
SSLVerifyDepth 10
SSLVerifyClient require
</Location>
RewriteRule /openam/UI/Login/PKI balancer://mycluster/openam/UI/Login?module=PKI [P]
...
and it can do the trick, but the cost is rewrite of
/openam/UI/Login?module=PKI
to
/openam/UI/Login/PKI
and I don't like it.
Can you advice me how to do it without this rewrite?
Thanks.
With apache2.4 I can do it with:
<If "%{QUERY_STRING} =~ /module=PKI/">
RequestHeader set X-Client-Cert "%{SSL_CLIENT_CERT}s"
SSLVerifyDepth 10
SSLVerifyClient require
<Else>
RequestHeader set X-Client-Cert ""
</If>
I think this is possible using a combination of SetEnvIf and RequestHeader.
Use SetEnvIf to set a variable indicating that the URI is for the right path:
SetEnvIf Request_URI "/openam/UI/Login?module=PKI" x_client_cert=1
And use the optional env=[!]variable parameter to RequestHeader:
RequestHeader set X-Client-Cert "%{SSL_CLIENT_CERT}s" x_client_cert=1
I've probably got the syntax slightly wrong -- in particular possibly the escaping and format of the second (URI) parameter to SetEnvIf, but this approach should work.
Documentation links:
http://httpd.apache.org/docs/2.2/mod/mod_setenvif.html#setenvif
http://httpd.apache.org/docs/2.2/mod/mod_headers.html#requestheader