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.
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 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
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 am trying to make our htaccess file as dynamic as possible, and wish to allow indexing of our .com site, but not of our .info site. We are using the same htaccess on both servers.
Is there an easy way to conditionally decide this in htaccess?
something like this?
If the domain does not end with "." and 3 letters then noindex nofollow?
RewriteCond %{HTTP_HOST} !^[^.]+\.[^.]{3}$
Header set X-Robots-Tag "noindex, nofollow"
Thanks for any input/help!
You can take help of mod_setenvif here:
# make sure it is not .com
SetEnvIf Host \.[^.]{4,}$ ROBOTAG
# set header if ROBOTAG is set
Header set X-Robots-Tag "noindex, nofollow" env=ROBOTAG
Here was the solution I eventually came up with, taking from another answer here I found, and modifying its regex, this is currently working for out deployment needs.
#modify query string condition here to suit your needs
RewriteCond %{HTTP_HOST} ^(.*).info [NC]
RewriteRule .* - [E=MY_SET_HEADER:1]
#if MY_SET_HEADER is present then set header
Header set X-Robots-Tag "noindex, nofollow" env=MY_SET_HEADER
I've got problem, I want to prevent hotlinking from some subdomain in specified directory.
Let's say, that we've got images at:
http://s1.domain.com/media/image1.jpg
http://s1.domain.com/media/image2.jpg
As default it is access to these locations, and users can see this photos. I want to prevent
from hot linking, but with no error message, but with redirection, so if user put this address into browser (http://s1.domain.com/media/image1.jpg), i want to redirect it do PHP script, as: domain.com/filename/image1.jpg. I want to make it with .htaccess. So please, can you give me code to put into this file.
Thanks for responses!
in the httpd.conf try this..
SetEnvIfNoCase Referer "^http://www\.yourdomain\.com/" banimages=1
SetEnvIfNoCase Referer "^http://yourdomain\.com/" banimages=1
SetEnvIfNoCase Referer "^$" banimages=1
<FilesMatch "\.(gif|png|jpe?g)$">
Order Allow,Deny
Allow from env=banimages=1
</FilesMatch>
or
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule ^.*\.(bmp|tif|gif|jpg|jpeg|jpe|png)$ - [F]
or some other techniques also available..