I am runing apache and I am trying to set a header Foo=bar only when the request has a variable "q" on the query string. I would like something like this in my htaccess:
<RequestUri "q=">
Header set Foor "bar"
</RequestUri>
Of course it does not work. I already tried using "Location" and "LocationMatch" but those are not allowed inside the htaccess. So how can I do that?
If you are using Apache 2.4, you can do something like this
<If "%{QUERY_STRING} =~ /q=.*?/">
Header set Foo "bar"
</If>
https://httpd.apache.org/docs/2.4/mod/core.html#if
https://httpd.apache.org/docs/2.4/expr.html#examples
Just to expand on this further if you want to set the header's value dynamically according to the value of the query string you can capture regex groups.
For example if you have a "_locale" variable in your URL and you want to capture its value for a header you could use:
<If %{QUERY_STRING} !~ m#_locale=([a-z]{2})#">
RequestHeader set locale "$1"
</If>
(Assuming the value will be two lower case letters.)
This could match "_locale=en" in the query string then copy "en" to header "locale".
Related
I would like to know how you can filter out a multiple list of URL, and redirect to a page .
Example:
www.domain.de/notuse/seite1.html
www.domain.de/dir1/notuse/seite1.html
www.domain.de/dir1/dir2/notuse/seite1.html
I want now " notuse " have filtered and since I am total beginner , I'm not sure if that is correct :
RedirectMatch permanent ^.+notuse/ http://www.domain.de/new-directory/index.html
What would you suggest?
change your regex patterns :
RedirectMatch ^/.*notuse/site1\.html$ http://domain.de/new-dir/index.html
.* matches any charecters zero more times, so here it would match /dir1/dir2 if present in Request_uri.
I'm currently trying to create a .htaccess file that essentially converts this:
[From] http://www.example.com/pagename.php?1=name&2=email&3=hash
[To] http://www.example.com/pagename/name/email/hash
Which can then be read in PHP as $name = $_GET[1]; $email = $_GET[2] and so on...
Where pagename is equal to the filename without the file extension and then every trailing slash after that is set as a new GET variable incrementing by one (in a way that you could theoretically define unlimited trailing slashes and it would continue to increment these $_GET variables by one.
Anyone know how?
If you want there to be an arbitrary number of variables, you need to turn on Multiviews then add some code to your php scripts to look at the $_SERVER['PATH_INFO'] variable. So something like this:
$data = explode("/",trim($_SERVER["PATH_INFO"],"/"));
$length = count($data);
for ($i = 1; $i <= $length; $i++) {
$_GET[$i] = $data[$i-1];
}
to populate the $_GET variable with all the path elements.
Then in htaccess, you need something like this to append the php extension:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)(/.+)$ /$1.php$2 [L]
I have tried the following:
RewriteCond %{QUERY_STRING} ^forum/profile$
RewriteRule http://example.com/example.htm? [L,R=301]
If the URL contains "forum/profile" I need it to redirect.
What am I doing wrong? I don't need to escape the forward flash do I?
Why are you using the ^ and $ operators? These operators means that you want match a string that must have the exact forum/profile on the entire string.
eg:
var query_string = forum/profile/something-here
var matches = string.match(^forum/profile$, string) // false because you have string before and after you pattern.
Also, you should check what exactly is given on QUERY_STRING.
Try to add the forward slash before the file name.
Eg;
"/forum/profile.php"
Or use "/?" Before you use "$"
You need to use an absolute path.
Hope this answered your question.
Another question about htaccess
This rule:
RewriteRule ^page(.*)/(.*)$ mainpage.php?var1=$1&var2=$2
simply does not work as it should. When I visit /page/var=something&var2=something I get an empty value for var1 and a value for var2.
RewriteRule ^page(.*)/(.*)$ mainpage.php?var=$1&var=$2
Explanation of what it does:
When you go to /page/var=something&var2=something, apache replaces with you're given RegExp and makes it something like this: mainpage.php?var=&var=var%3Dsomething%26var2%3Dsomething. So, var will be an empty string (length is zero). By the way, you have two var in your query string!
Edit: Note that in example above (/page/var=something&var2=something) $1 would be anything between /page and /var , and since nothing's there, you'll have an empty string!
Edit 2:
RewriteRule /?page/var\=([^&]*)&var2\=([^&]*) mainpage.php?var1=$1&var2=$2
Is there a way to read the value of a request header in Apache config? I want to avoid going into the .htaccess file and even better if I could avoid using SetEnvIf.
I'm trying to implement the suggestion here: https://stackoverflow.com/a/1850482/138228
I can't really find much on this topic outside of using regex. What I'm looking for is something like :
Header set NAME = %{value_of_different_header}%
Reading answer from : https://serverfault.com/questions/136428/header-set-access-control-allow-origin-not-working-with-mod-rewrite-mod-jk
It seems the solution is:
SetEnvIf Origin "http(s)?://(domaine1\.com|domain2\.com)$" AccessControlAllowOrigin=$0
Header always set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
But this has nothing to do with .htaccess, you can put such lines in regular apache configuration file (virtualhost?) without using the bad .htaccess dynamic configuration files.
The value part of the Header instruction is:
value may be a character string, a string containing format specifiers or a combination of both
But the only format specifiers available are %% (for %), %t for a timestamp, %D (idem), %{foo}e and %{foo}s for environments variables.
SetenvIf is a good apache tool to read a request header, here reading the Origin header. Then, using environment variables is the classical way of writing complex things in Apache (I mean the way to store some sort of variables).
So I don't know why you would like another solution.
I couldn't find a away of doing this without SetEnvIf but other than that just copying a different header can be done with the following, I was doing this as part of a ReverseProxy (hence using RequestHeader instead of Header) but the two appear to be interchangeable
###This reads the value of OldHeader into TempValue
SetEnvIf OldHeader ".+" TempValue=$0
###This will overwrite the value of "NewHeader" if it was already set
RequestHeader set NewHeader %{TempValue}e env=TempValue
###Optionally remove the original
RequestHeader unset OldHeader