Referer

Discuss practical ways rearrange URLs using mod_rewrite.

Referer

Postby rile » Tue Oct 21, 2008 12:54 pm

Hi,
For some "stupid" reasons I must do following: deny access to one file if referrer is in list. Because list is not long (few sites), I defined them with
RewriteCond %{HTTP_REFERER} domain\.tld [NC,OR]

And problem is in defining RewriteRule.

Referring site call http://mysite.tld/file.php?q=something

I tried almost all combinations to match this expression, but nothing work. Only what I make to work is RewriteRule .* - [F]

Please help!
rile
 
Posts: 2
Joined: Tue Oct 21, 2008 12:33 pm

Postby laisfun » Tue Oct 21, 2008 11:31 pm

RewriteCond %{HTTP_REFERER} ^http://(www\.)?example\.(com|net) [NC]
RewriteRule ^file\.php\?q=something$ - [F]

or

RewriteRule ^file\.php - [F,L]

---------------------------------------
Don't use the [OR]

You'll have to change the word 'file' & 'something' to the correct file & query word you're attempting to match. Remember, http_referer can be bypassed
by modifying the browser headers.

Also, you can use just the 2nd Rewrite option if the query word isn't necessary to match.
Last edited by laisfun on Wed Oct 22, 2008 11:04 am, edited 1 time in total.
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby richardk » Wed Oct 22, 2008 7:19 am

To match a query string you must use the %{QUERY_STRING} variable.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_REFERER} ^http://(www\.)?example\.com(/.*)?$ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?example\.net(/.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?q=something(&.*)?$ [NC]
RewriteRule ^file\.php$ - [F,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby laisfun » Wed Oct 22, 2008 11:18 am

Richards Response is much better :-)
You can also tighten the code a bit by removing
one of the RewriteConditions...

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_REFERER} ^http://(www\.)?example\.com|net [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?q=something(&.*)?$ [NC]
RewriteRule ^file\.php - [F,L]


The first RewriteCond should work, if not just add a parenthesis around
the (com|net)
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby rile » Wed Oct 22, 2008 1:25 pm

Thanks for help!!!
rile
 
Posts: 2
Joined: Tue Oct 21, 2008 12:33 pm

Postby richardk » Wed Oct 22, 2008 1:40 pm

You can also tighten the code a bit by removing
one of the RewriteConditions...

The point of the two domains was to show how it could be used to block multiple domains. You should not remove (/.*)?$ because without it it will match example.com.good.domain.tld not just example.com.

If you want to match many domains in one condition
Code: Select all
RewriteCond %{HTTP_REFERER} ^http://(www\.)?(example\.com|another\.local)(/.*)?$ [NC]


The first RewriteCond should work, if not just add a parenthesis around
the (com|net)

You do need () around com|net.

I tried every combination, but I don't get code 500 in logs as I get with RewriteRule .* - [F] for that requests.

The F flag creates a 403 (forbidden) error. Are you not getting a forbidden error when following a link on the disallowed sites?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby laisfun » Wed Oct 22, 2008 2:18 pm

You do need () around com|net.


But the odd thing is, my regex tester and a test done on my website
confirmed that com|net without the parens worked. I was a bit
surprised that it works without the parens since in the past I would
always use the parens.
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby richardk » Wed Oct 22, 2008 2:28 pm

You are correct. I incorrectly though the ^ would count for both, but it is only from the first part, making it
Code: Select all
^http://(www\.)?example\.com

or
Code: Select all
net

(not a good regular expression anyway).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 19 guests

cron