I really can't find a solution, can someone pleas tell me how to block the following specific user agent exactly via isapi rewrite 3.0?
I want to block:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101
Firefox/28.0
Current solution, but doesn't work:
RewriteCond %{HTTP:User-Agent} ^Mozilla/5.0\ (Windows\ NT\ 6.1;\ WOW64;\ rv:28.0)\ Gecko/20100101\ Firefox/28.0 [NC]
RewriteRule .? - [F]
I tried so many ways but it doesn't seem to work...
Please help!
You need to escape the ( and ) characters in your UA string:
RewriteCond %{HTTP:User-Agent} ^Mozilla/5.0\ \(Windows\ NT\ 6.1;\ WOW64;\ rv:28.0\)\ Gecko/20100101\ Firefox/28.0 [NC]
RewriteRule .? - [F]
( and ) are special characters in Regex
Related
Yesterday I discovered that my server's response to non-supported HTTP methods was incorrect. If an HTTP method is not supported you're supposed to respond with an HTTP 405: Method Not Supported. Pandurang was able to answer that question.
I've done a lot of reading about how [L] loops and I had spent time going through all the Apache 2.4 flag documentation.
I tried using the [L] flag and then the [END] flag.
I tried to combine flags in every possible way (e.g. [L,R=405], [END,R=405]) on both RewriteRule sets.
I tried combinations where flags where absent.
I rearranged the order of the rules.
I tried adding RewriteCond %{REQUEST_METHOD} ^(GET|POST)$ before the unrelated second RewriteRule.
I did a lot of reading.
Here are the two set of rules:
RewriteEngine On
RewriteCond %{REQUEST_METHOD} !^(GET|POST)$
RewriteRule .* - [R=405]
#Unrelated rule, when present, breaks the above HTTP 405 turning it in to an HTTP 302:
RewriteRule !\.(css|js|zip)$ index.php [L]
To do testing I ran the following command using cURL:
curl -X PUT -d arg=val -d arg2=val2 https://www.example.com/ -i
I know the HTTP 405 code works via cURL because I can see the response code, just not when the other line is present and I have to have both. What do I need to do to force Apache to stop looping/processing once it matches a rule?
For some reason the new code must be at the bottom, which makes no sense as I'd like to avoid having Apache process all of the other rules to only end up doing this.
#Unrelated rule
RewriteRule !\.(css|js|zip)$ index.php [L]
#Rule that now works, only at the bottom
RewriteCond %{REQUEST_METHOD} !^(GET|POST)
RewriteRule .* - [R=405,L]
I'm trying to work out why this doesn't work:
RewriteRule ^search/(.*)-more([0-9]+).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1;nh=$2 [P,L]
RewriteRule ^search/(.*).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1 [P,L]
Example usage:
http://foo.com/search/test.html
http://foo.com/search/test-more-2.html
http://foo.com/search/test%20extra.html
http://foo.com/search/test%20extra-more-2.html
The first 2 work fine - test gets passed along to the query param.
However, on the 2nd one - it gets cutoff at %20 ... so instead of passing test%20extra, all it passes is test
I've compared a similar rule I'm using on another server, and it works fine with (.*) as the selector... so I'm not sure whats different here!
Any suggestions?
Argh... I got it! It wanted NC,L and not P,L as the flags. So:
RewriteRule ^search/(.*)-more([0-9]+).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1;nh=$2 [P,L]
RewriteRule ^search/(.*).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1 [P,L]
Needed to be:
RewriteRule ^search/(.*)-more([0-9]+).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1;nh=$2 [NE,L]
RewriteRule ^search/(.*).html /cgi-bin/search.cgi?bool=and&substring=0&query=$1 [NE,L]
I am trying to set a simple replacement in my URL query strings.
I have an absolute path in my query string I would like to be replaced by a custom string.
FROM http://acme.com/a/path?file=DIR/this.file&foo=2
TO http://acme.com/a/path?file=/long/absolute/path/to/this.file&foo=2
These are my directives:
# [sudo a2enmod rewrite]
RewriteCond %{QUERY_STRING} ^(.*)file=DIR(.*)$
RewriteRule ^/a/path /a/path?%1file=/long/absolute/path/to%2
With such a configuration, replacement does not work, DIR passes through.
I configured a LogLevel to trace1 in my Apache2 configuration but I get nothing in my access.log and no useful feedback in error.log.
The question is:
References:
https://wiki.apache.org/httpd/RewriteQueryString
https://httpd.apache.org/docs/current/rewrite/remapping.html
This works for me(I've added flags R and L):
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)file=DIR(.*)$
RewriteRule ^/a/path /a/path?%1file=/long/absolute/path/to%2 [R,L]
See also https://httpd.apache.org/docs/2.4/rewrite/flags.html
I am having a bit of difficulty getting mod_rewrite to do what I need it to do.
We have a group of virtual subdomains in a Drupal install. So, academics.univ.edu, about.univ.edu, etc are all part of the same core Drupal install.
File access currently is academics.univ.edu/sites/all/academics/files/myfile.jpg. However this path will also work as about.univ.edu/sitse/all/about/files/myfile.jpg or any other valid subdomain.
We'd like to use mod_rewrite to accept academics.univ.edu/files/myfile.jpg and deliver the file from the above location.
Here's what I've tried:
RewriteCond %{REQUEST_URI} ^(about|academics|bursar|calendar)\.univ\.edu\/files\/(.*)$ [NC]
RewriteRule ^/sites/all/files/$1/$2 [L,NC]
I'm probably going about this the wrong way, but I wanted to check on it. I can get the subdomains to work by making separate rules using HTTP_HOST, but I wanted less rules in the file. Also, I can't get HTTP_HOST to work on sites that exist as a subdirectory in a subdomian. For instance, undergrad.univ.edu/biology/files/myfile.jpg should deliver /sites/all/biology/files/myfile.jpg
You can't match a host in the %{REQUEST_URI}, you need to use %{HTTP_HOST}, then use the %1 backrefernce to access that match. The actual URI can be matched in the rule itself. Something like this:
RewriteCond %{HTTP_HOST} ^(about|academics|bursar|calendar)\.univ\.edu$ [NC]
RewriteRule ^files/(.*)$ /sites/all/files/%1/%2 [L,NC]
The %1 references the match (about|academics|bursar|calendar) in the RewriteCond and the $1 references the match (.*) in the RewriteRule. So that example will take a request to http://about.univ.edu/files/foo.html and rewrite the request to /sites/all/files/about/foo.html.
Also, if this is in a virtualhost or server config, you need a "/" in between "^" and "files" in the RewriteRule.
I'm at my wits end here , I normally like to work things out on my own but this has me well and truly beaten here..
I'm trying to mod rewrite my urls that contain pluses...
/search.php?q=can+be+any+length
to
/can-be-any-length.html
Any help would be really appreciated becaus rewriting the + php is not an option
Using mod_rewrite for this kind of work is not the best option as you can only replace a fixed amount of characters at at time. Using PHP would certainly be easier.
But you can use the "N" (Next) flag to restart replacement, causing the rewrite engine to loop while there's a "+" in the query string:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)q=([^&+]*)\+([^&+]*\+.*)
RewriteRule ^ %{REQUEST_URI}?%1q=%3-%4 [N]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)q=([^&+]*)\+([^&+]*)&*(.*)$
RewriteRule ^ /%3-%4.html?%1%5 [L,R=301]
Maybe with RewriteMap :
RewriteMap mymap txt:/path/to/file.txt
RewriteRule ^(.*).html$ search.php?q=${mymap:$1}
Content of file /path/to/file.txt
- +