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+)
Related
I have a client request that cannot contains custom headers, but server-side I need a hearder (let's call him foo).
So I have this url path/to/bar?foo=value
I want that my apache conf that the foo value and put it in a header.
I haven't so far find a solution :(.
Finaly I find a solution
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(?:.*&)?foo=(.*)$
RewriteRule ^ - [env=foo:%1]
<IfDefine !foo>
RequestHeader set Foo %{foo}e env=foo
</IfDefine>
The following RewriteRule in my htaccess file isn't getting the request header set.
Header set Access-Control-Allow-Origin "*"
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)&someUser=(.*)$
RewriteRule ^(.*)SDM$ http://some.domain.com/SDM/Publish.aspx [E=SOME:%2,R,L]
RequestHeader set Some-User: "%{SOME}e"
I don't think the SOME environment variable has anything to do with it because I tried a generic header value as well and it wasn't set either. I did make sure that mod-headers is installed. I am looking for the header in my chrome developer tools. Is it possible that it won't show up there?
Env variables won't be set while doing external redirect, you must do internal rewrite for setting env variables like this:
Header set Access-Control-Allow-Origin "*"
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)someUser=([^&]+) [NC]
RewriteRule ^(.*)SDM$ /SDM/Publish.aspx [E=SOME:%2,L]
RequestHeader set Some-User "%{SOME}e"
How do you access the query string from SetEnvIf? Somethig like:
SetEnvIf Query_String "p=path/to/file$" got_path
UPDATE:
In htaccess, I have:
SetEnvIf Request_URI !/folder/page1\.html$ NO_COOKIE
Header unset Cookie env=NO_COOKIE
RewriteRule (.*) /h.php?ref=$1 [L]
Basically, I ask h.php to take control of all user requests. And I use SetEnvIf to allow cookies only for /folder/page1.html.
However, it seems like Request_URI is always set to "h.php" and never to " /folder/page1.html" (maybe because of the redirection). For that reason I added ref=$1 to try to recognize which url it is being redirected from. Therefore I need to read the query string from SetEnvIf.
I hope I am making some sense.
You don't need to add a query string for this.
You can use:
# always start with NO_COOKIE=1
RewriteRule ^ - [E=NO_COOKIE:1]
# unset NO_COOKIE when URI is /folder/page1.html
RewriteCond %{THE_REQUEST} /folder/page1\.html
RewriteRule ^ - [E=!NO_COOKIE]
Header set NoCookie %{NO_COOKIE}e
RequestHeader set NoCookie %{NO_COOKIE}e
I need to take a parameter from the query string and set it in the referrer header in the Apache configuration file.
Do you know if this is possible?
I'm able to do the same with the cookies but I need to do it using the query string.
setEnvIfNoCase ^Cookie$ "(referrer=\w*:\/\/\w*)" HTTP_MY_COOKIE=$1
setEnvIfNoCase HTTP_MY_COOKIE "(http:\/\/.*\.\w*)" REFERRER=$1
RequestHeader set Referer %{REFERRER}e
Regards
The solution was quite simple.
setEnvIfNoCase or setEnvIf can't use the query string so is not possible to use the same trick i used for the cookies, the solution is a combination of RewriteCond with RewriteRule like in the example below:
RewriteCond %{QUERY_STRING} referrer=(.*)
RewriteRule ^/ - [env=REFERRER:%1]
RequestHeader set Referer %{REFERRER}e
i answered to my question so another user (if exist) with the same question can find an starting point.
I have a setup where I have servers like this:
load balancer -> Apache -> Tomcat
I would like Apache to write the url that the client used into a header, so I can read that once I hit tomcat.
I've tried to use mod_rewrite and mod_headers so do it, but with no luck.
if I look at http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html then it seems clear that i need the variable called SCRIPT_URI:
SCRIPT_URI=http://en1.engelschall.com/u/rse/
I also looked at this http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html so figure out how to write headers and have had some succes, but not enough.
I have php installed on the apache server, and if i look at phpinfo() i can see the SCRIPT_URI is there and has a sensible value.
I just can't get it to write it to a header. Here's a simplified version of what I've done:
#load modules
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule headers_module modules/mod_headers.so
#Get the original uri used
RewriteRule .* - [E=INFO_SCRIPT_URI:%{SCRIPT_URI},NE]
RequestHeader set x-orig-uri "%{INFO_SCRIPT_URI}e"
I've tried several other options and both on windows, cygwin and ubuntu linux
Any ideas?
I found a workaround myself, although it's not the clean and easy solution I wanted.
I was able to get the indvidual parts and can use them to build the full URI:
<IfModule !rewrite_module>
LoadModule rewrite_module modules/mod_rewrite.so
</IfModule>
<IfModule !headers_module>
LoadModule headers_module modules/mod_headers.so
</IfModule>
<IfModule rewrite_module>
<IfModule headers_module>
####### INITIAL SETUP #########################
RewriteEngine on
####### SET HEADERS #########################
#get and set the host name
RewriteRule .* - [E=INFO_HTTP_HOST:%{HTTP_HOST},NE]
RequestHeader set x-orig-host "%{INFO_HTTP_HOST}e"
#get and set the host port
RewriteRule .* - [E=INFO_SERVER_PORT:%{SERVER_PORT},NE]
RequestHeader set x-orig-port "%{INFO_SERVER_PORT}e"
#If the uri starts with a slash and some alphanumerics, then make a
#group of that until the first non-alpha (ie. the next slash)
RewriteCond %{REQUEST_URI} ^(/[\w-]+)
#Save the content of the regex match group ( %1 ) in an environment variable
RewriteRule .* - [E=INFO_REQUEST_CONTEXT:%1,NE]
#Set a header with the content of the environment variable
RequestHeader set x-orig-context "%{INFO_REQUEST_CONTEXT}e"
#If the accept-header contains a number after ;version= then make a regex group of that number
RewriteCond %{HTTP_ACCEPT} \+json;version=(\d+)$
#Save the content of the regex match group ( %1 ) in an environment variable
RewriteRule .* - [E=INFO_ACCEPT_VERSION:%1,NE]
#Set a header with the content of the environment variable
RequestHeader set x-orig-accept-version "%{INFO_ACCEPT_VERSION}e"
#If the accept-header contains kasia2. followed by some letters,
#then make a regex group of those letters
RewriteCond %{HTTP_ACCEPT} kasia2.(\w+).*
#Save the content of the regex match group ( %1 ) in an environment variable
RewriteRule .* - [E=INFO_ACCEPT_NAME:%1,NE]
#Set a header with the content of the environment variable
RequestHeader set x-orig-accept-name "%{INFO_ACCEPT_NAME}e"
#If https is on ...
RewriteCond %{HTTPS} on
#...then set the protocol environment variable to "https"
RewriteRule .* - [E=INFO_PROTOCOL:https,NE]
#If https is off ...
RewriteCond %{HTTPS} off
#...then we assume it must be "http"
RewriteRule .* - [E=INFO_PROTOCOL:http,NE]
#Finally, set the protocol header
RequestHeader set x-orig-protocol "%{INFO_PROTOCOL}e"
#Get the request uri and set an environment variable
RewriteRule .* - [E=INFO_REQUEST_URI:%{REQUEST_URI},NE]
#Build the whole original url out of the available parts. SCRIPT_URI is always null, otherwise we could have used that.
RequestHeader set x-orig-url "%{INFO_PROTOCOL}e://%{INFO_HTTP_HOST}e%{INFO_REQUEST_URI}e"
#In addition make an url with only the host and context, for convenience
RequestHeader set x-orig-url-base "%{INFO_PROTOCOL}e://%{INFO_HTTP_HOST}e%{INFO_REQUEST_CONTEXT}e"
</IfModule>
</IfModule>
According to https://serverfault.com/questions/23470/setting-apache-environment-variable-on-mod-rewrite-rewrite-condition, you need to tweak the order of execution by using different configuration sections
Here I added a dummy <Location> section
RewriteRule ^ - [E=FOO:set_by_rewrite_rule]
RequestHeader set x-foo-outside-location %{FOO}e
<Location "/bla">
RequestHeader set x-foo-in-location %{FOO}e
</Location>
and these are the headers that I'm getting
x-foo-in-location='set_by_rewrite_rule'
x-foo-outside-location='(null)'
Note that I get both headers to null if I use <Location "/"> (and that I'm unable to find a solution valid for all request paths)
Note also that Sebastian's answer is getting the env variables in the header without any dummy configuration section... Go figure!