Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying rewrite to prevent hotlink access to certain php files only. 1

Status
Not open for further replies.

nerbonne

Technical User
Dec 11, 2006
99
0
0
US
Hi,

I've tested this hotlinking code in my htaccess file and it works good. The problem is that it blocks all files of the type specified. Can anyone tell me how to modify it to block a certain file only?

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(php)$ /index.php [L]
 
Hi

Then put the name there, instead of the asterisk ( * ) expression.
Code:
[small]RewriteEngine On
RewriteCond %{HTTP_REFERER} !^[URL unfurl="true"]http://(.+\.)?mysite\.com/[/URL] [NC]
RewriteCond %{HTTP_REFERER} !^$[/small]
RewriteRule [red]^/path/to/certain[/red]\.php$ /index.php [L]

Feherke.
 
That worked great, but, can I add a second line that does restrict images from being hotlinked? I mean, I got it to work, but when the main site loads, the images are broken and the .css doesn't load. Does it matter that the images are in a subdirectory?
 
Hi

I do not understand you. That [tt]RewriteRule[/tt] blocks only one PHP file. Unless that PHP file is called on requests for images and styles, it has nothing to do with them. There must be other reason for the missing images.

Feherke.
 
Like this:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule ^details\.php$ /index.php [L]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /index.php [L]


Both rules work. When I hotlink to details.php, I get the homepage just fine. When I hotlink to site.com/subfolder/image.jpg, I get the homepage, but all the images are broken.

Obviously this is because you can't hotlink images, so how can I restrict the hotlinking ban to the subfolder images only.
 
Hi

That will not work, because the [tt]RewriteCond[/tt] rules are applied only to the very next [tt]RewriteRule[/tt] and not to the following ones.

You can either duplicate the [tt]RewriteCond[/tt]s before the second [tt]RewriteRule[/tt] or use only on [tt]RewriteRule[/tt] with an expression witch contains both conditions.
Code:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^[URL unfurl="true"]http://(.+\.)?mysite\.com/[/URL] [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule ^details\.php$ /index.php [L]
[red]RewriteCond %{HTTP_REFERER} !^[URL unfurl="true"]http://(.+\.)?mysite\.com/[/URL] [NC]
RewriteCond %{HTTP_REFERER} !^$[/red]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /index.php [L]

[gray]# or[/gray]

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^[URL unfurl="true"]http://(.+\.)?mysite\.com/[/URL] [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule ^[red]([/red]details\.php[red]|[/red].*\.(jpe?g|gif|bmp|png)[red])[/red]$ /index.php [L]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top