dslreports logo
site
 
    All Forums Hot Topics Gallery
spc

spacer




how-to block ads


Search Topic:
uniqs
1161
share rss forum feed


Nordy

join:2007-10-20
kudos:1

RewriteRule failing when using HTTP_HOST

I did this rule which I hoped will redirect all users who try to access a .php file who are not from our host (since HTTP_REFERRER can be blank). But it wont work.

RewriteCond %{HTTP_HOST} !^localhost[NC]
RewriteRule \.(php)$ http://www.google.com [NC,R,L]
 
Over here, If : the host is not localhost, and the page extension is .php, send the user away

But thing is, even if the host "is" localhost, it'll send the user away.

My HTTP_HOST is localhost when i do this:

<?php
 
echo $_SERVER['HTTP_HOST']; //Gives me "localhost"
 
?>
 

Can u help me do this right?


state
stress magnet
Premium,Mod
join:2002-02-08
Purgatory
kudos:6

You need to lose the conditional arguments on the RewriteCond line. Try this:

RewriteCond %{HTTP_HOST} !^localhost$
RewriteRule \.(php)$ http://www.google.com [NC,R,L]
 


Nordy

join:2007-10-20
kudos:1

Wow! That actually worked. I guess Apache was returning the HTTP_HOST in uppercase or mixed case.

But does this mean I'll have to remove conditions for other rules too?
Like:

RewriteCond %{HTTP_REFERER} !^$  
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]  
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain2.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://www.localhost.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://www.google.com [NC,R,L]
 

I've not yet tested this on a server.
Thank you for helping with this.


state
stress magnet
Premium,Mod
join:2002-02-08
Purgatory
kudos:6

The 'no case' statements shouldn't be necessary since it's a domain name which is lowercase. The problem you had before was the space missing between your expression and the NC flag. You can leave them in, or remove them, shouldn't really matter in this case.