I would like to know if there is a way to block all user-agents except the one that contains the word "chrome" using .htaccess
I used something like this, but this works unfortunately only if the exact name is given..
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !Lynx/2\.8\.8dev\.12 [NC]
RewriteRule ^ - [F,L]
You can just use:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !chrome [NC]
RewriteRule ^ - [F]
Related
I have a page that is www.example.com/page.html with a link on it that goes to on a blank page www.example.com/folder/link.php?id=123456
For a specific reason, when I'm on the same page but in other language (page will be like www.example.com/page_ro.html) I cannot change this link and need to use rewrite condition to do change that same link.
Let say I need the link to become www.example.com/folder/link.php?id=78910 in this case.
How can I do that in this case ? I've tried this with no success :
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} ^http://www\.example\.com/page_ro.html [NC]
RewriteCond %(REQUEST_URI) folder/link\.php?id=123456$ [NC]
RewriteRule ^(.*) http://www.example.com/link.php?id=78910 [L]
You cannot match query string using REQUEST_URI variable. Use QUERY_STRING instead like this inside /folder/.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder/
RewriteCond %{HTTP_REFERER} ^http://www\.example\.com/page_ro.html [NC]
RewriteCond %(QUERY_STRING) ^id=123456 [NC]
RewriteRule ^(link\.php)$ $1?id=78910 [NC,L,R=302]
I'm trying to get a series of rewrite conditions working, with the logic being this
if condition1 or
(condition2 and condition3) or
..
This is what I have in the .htaccess:
RewriteCond %{HTTP_USER_AGENT} "iphone" [OR,NC]
RewriteCond %{HTTP_USER_AGENT} "android&mobile" [OR,NC]
RewriteCond %{HTTP_USER_AGENT} "iemobile" [NC]
Unfortunately, it looks like the and operator isn't working as I thought it would.
(as you might guess, the idea is to detect android phones but not tablets)
Is there a way to write that and condition to achieve the results I'm looking for?
Thanks.
Unfortunately, the [OR] flag doesn't work as nice enough for it to be useful, it only works for either all "or"'d or all "and"'d conditions. It's not very predictable. What you may need to do is separate them out to several rules and either use the S flag to skip stuff or the pass-through.
Maybe something along the lines of:
# Prevent rewrite looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# if "iphone" OR
RewriteCond %{HTTP_USER_AGENT} "iphone" [NC]
RewriteRule ^ - [S=3]
# "android" AND "mobile", OR
RewriteCond %{HTTP_USER_AGENT} "android" [NC]
RewriteCond %{HTTP_USER_AGENT} "mobile" [NC]
RewriteRule ^ - [S=2]
# "iemobile"
RewriteCond %{HTTP_USER_AGENT} "iemobile" [NC]
RewriteRule ^ - [S=1]
# skip everything, none of the conditions match
RewriteRule ^ - [L]
# apply the rule
RewriteRule ^ /do-something [L]
Looks like a mess but that's mod_rewrite for you.
The first rule is to prevent any sort of internal rewrite looping. The "# skip everything" rule is the one that gets applied if none of the 3 conditions match, it essentially does nothing excepts stops any rewriting. If you have other rules after all of this stuff that you want to get applied, you can replace the L flag with S=1.
The last rule is the rule that gets applied if any of the 3 conditions matches.
We distribute different versions of a software product through a single download link. The delivery is based on the referer in conjunction with a default value, which works fine. In addition the user should be redirected to a 404-page, in case the wrong filename was used.
At the moment the .htaccess-file looks like this:
# stop directory listing
Options -Indexes
# turn rewrite engine on
RewriteEngine On
# force 404 if file name is missing or wrong
RewriteCond %{REQUEST_URI} !^(download_mac\.zip|download_pc\.zip)$
RewriteRule (.*) 404/index.html [L]
# an example based on the referer
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-a\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-b\.com
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ domain_ab/$1 [L]
# last rule if no referer matches
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ default/$1 [L]
So I have one issue and one additional question with this file:
The first rule, to force 404, is very greedy and gets the error page every time, no matter what URL is called. I also tried single statements like RewriteCond %{REQUEST_URI} !^download_mac\.zip$ without any effect. How can I fix this?
How can I get rid of the filenames in any other rule? I tried things like RewriteRule ^(.*)$ default/$1 [L] but it gives me a hard time and an 500 Internal Server Error.
You can avoid repeating your filenames by using an Env variable like this:
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ - [E=ALLOWED:$1,NC]
RewriteCond %{ENV:ALLOWED} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /404/index.html [L]
RewriteCond %{ENV:ALLOWED} !^$
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-a\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-b\.com
RewriteRule ^ /domain_ab/%{ENV:ALLOWED} [L]
RewriteCond %{ENV:ALLOWED} !^$
RewriteRule ^ /default/%{ENV:ALLOWED} [L]
You can just move the rewrite rule to the end. The other rules handle the valid cases and if none of them matches the last rule applies
# an example based on the referer
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-[ab]\.com
RewriteRule ^download_(mac|pc)\.zip$ domain_ab/$0 [L]
# last rule if no referer matches
RewriteRule ^download_(mac|pc)\.zip$ default/$0 [L]
# force 404 if file name is missing or wrong
RewriteRule ^ 404/index.html [L]
I want to allow access to specific domains. For example if domain contains the word asdf it should allow access. I final attempt before asking was:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^.*asdf.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} !^.*1234.*$
#RewriteRule .* - [F]
So here I tried to restrict access to all but domains that contain asdf or 1234.
You need to use %{HTTP_HOST} for checking the domain in URL instead of %{HTTP_REFERER}.
Can you try this code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^.*(asdf|1234)\. [NC]
RewriteRule .* - [F]
Anubhava gave me a clue but not with the http_host. Finally the problem was the OR.
Now the following worked like a charm:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^.*(1234|asdf).* [NC]
RewriteRule .* - [F]
So HTTP_REFERER did what it should do (check the domain accessing). And the | worked as the or argument I needed.
The following code works fine:
RewriteCond %{HTTP_REFERER} !^http://superwebx.com/.*$ [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png|swf|css)$ - [F]
but I want to make a generic script serve me for several sites I manage, but fails try to get
RewriteCond %{HTTP_REFERER} !^http://%{HTTP_HOST}/.*$ [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png|swf|css)$ - [F]
You can't use variables inside the regex. You can work around this by using a RegEx backreference like so:
RewriteCond %{HTTP_REFERER} ^https?://([^/]+)/ [NC]
RewriteCond %1#%{HTTP_HOST} !^(.+)#\1$
RewriteRule \.(jpe?g|gif|bmp|png|swf|css)$ - [F]
(note the # is just used as a boundry. It could be any character that isn't used in domain-names.)
Very old one, but here's your answer:
RewriteCond %{HTTP_HOST}##%{HTTP_REFERER} !^([^#]*)##https?://\1/.*