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.
Related
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]
This seems easy enough but I can't seem to find the exact solution online and to get it to work. I want to redirect all the links e.g.
user1.example.com
user2.example.com
user3.example.com etc.
to
example.com/search.html?user=user1
example.com/search.html?user=user2
example.com/search.html?user=user3 etc.
in my .htaccess I have
RewriteCond %{HTTP_HOST} !^(www\.)?example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([\.]+)\.example.com$ [NC]
RewriteRule .* /search.html?user=% [L]
I want to redirect all the user "subdomains" (they don't really exist on the server) except if the domain is prefix with www. The "user" field consists of alphabets, numeric, - (dash), _ (underscore) and possibly space. I am getting the Server Not Found error with the above .htaccess with for example, user1.example.com What am I doing wrong? Can this be done without the real existence of these third level domains? Thanks in advance.
You can use negative lookahead in your condition while capturing first part of domain and use the back-reference as %1 later:
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$ [NC]
RewriteRule !^search\.html$ /search.html?user=%1 [L,QSA,NC]
Try this :
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule .* /search.html?user=%1 [R,L]
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]
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/.*
I'm trying to achieve te following rewrite:
The url, stenenkermis.be or www.stenenkermis.be will result in www.stenenkermis.be/2012/v1/
www.stenenkermis.be/2012/v1/page/param/param/ needs to result in
www.stenenkermis.be/2012/page.php?param1=¶m2= (but the parameters are optionality the user can also go to home/ without parameters)
Currently this is my htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^stenenkermis\.be$
RewriteRule (.*) http://www.stenenkermis.be/$1 [R=301,L]
RewriteRule ^$ /2012/v1/ [L,R=301]
RewriteRule ^2012/v1/([^/]+)/ /2012/$1.php [NC,R]
RewriteRule ^2012/v1/([^/]+)/([^/]+)/([^/]+) /2012/$1.php?arg1=$2&arg2=$3 [NC,R]
RewriteRule ^2012/v1/([^/]+)/([^/]+)/([^/]+) /2012/$1.php?arg1=$2&arg2=$3 [NC,R]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?stenenkermis.be/.*$ [NC]
RewriteRule \.(gif|jpe?g|png)$ - [F]
the only problem is that my url gets redirected to
http://www.stenenkermis.be/2012/page.php?param1=¶m?= in the address bar ,I don't want the user to see this.
Can anyone help me with figuring this htaccess out?
Don't use R in your rewrite flags, since it means mod_rewrite is instructed to send an HTTP redirect to the user. Use QSA instead.
Fge is right, but your rules seem to be in the wrong order.
Rule of thumb: always start with the most complex the go to the less complex.
And of course, add QSA instead of R in the right places:
As a suggestion I've added all the static files (it's my rule in my prod environment):
Thus:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^stenenkermis\.be$
RewriteRule (.*) http://www.stenenkermis.be/$1 [R=301,L]
# Rewrite, but **keep on** with modified URL:
RewriteRule ^$ /2012/v1/ [NC,QSA]
RewriteRule ^2012/v1/([^/]+)/([^/]+)/([^/]+) /2012/$1.php?arg1=$2&arg2=$3 [NC,QSA,L]
RewriteRule ^2012/v1/([^/]+)/([^/]+) /2012/$1.php?arg1=$2 [NC,QSA,L]
RewriteRule ^2012/v1/([^/]+)/ /2012/$1.php [NC,QSA,L]
# operate with static files: forbid if not coming for original website:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?stenenkermis.be/.*$ [NC]
RewriteRule (.*)(\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico))$ - [NC,F]
And now two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)