Htaccess -less hardcoded

Hello,

I write server-side code.
There is a web-site which asks for a file named old.png
I want the server to check the reffere url. If it’s http://www.domain2.com/Bing I want it to redirect that request to another file in a different path, named new.png
For some odd reason I cannot make it rigt. could you plsease help me understand my mistake?

from fiddler:


GET http://www.domain1.com/images/old.png HTTP/1.1
Accept: */*
Referer: http://www.domain2.com/Bing
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; WOW64; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Accept-Encoding: gzip, deflate
If-Modified-Since: Wed, 27 Oct 2010 10:12:22 GMT
If-None-Match: "087e171bf75cb1:0"
Host: www.domain1.com
Connection: Keep-Alive

i guess the website address domain1 because it’s hard-coded path (My guess).

The strange thing is that i fired-up the whole scenario by tuping the url http://www.domain2.com/Bing , but the host is written:www.domain1.com so it’s all blur for me.

My htaccess file which is attaced to http://www.domain2.com/
via ISAPI (IIS)


RewriteEngine On
RewriteCompatibility2 On
RepeatLimit 32
RewriteBase

RewriteRule ^\\/Bing$ http://www.domain2.com [NC,L,P]

RewriteCond %{HTTP_REFERER} ^http://www.domain2.com/Bing [or]
RewriteCond %{HTTP_REFERER} ^http://www.domain2.com/bing
RewriteRule ^(.*)\\/ActivitiesService.asmx\\/GetLatest$ http://www.domain2.com/ActivitiesService.asmx/GetL... [NC,L,P]
RewriteRule ^http://www.domain1.com/images/old.png$ http://www.domain2.com/images/new.png [NC,L]

The rest is less relevant

Thanks for any asistance

RewriteConds only apply to the very first RewriteRule after not, not to other RewriteRules. That being said, it should be:


RewriteCond %{HTTP_REFERER} ^http://www.domain2.com/Bing [OR,NC]
RewriteRule ^(.*)\\/ActivitiesService.asmx\\/GetLatest$ http://www.domain2.com/ActivitiesService.asmx/GetL... [NC,L,P]

RewriteCond %{HTTP_REFERER} ^http://www.domain2.com/Bing [OR,NC]
RewriteRule ^http://www.domain1.com/images/old.png$ http://www.domain2.com/images/new.png [NC,L]

(I also replaced the two Conds with on, using [NC])

if so- then [OR] make no scene. no ?

True, yes, you can remove that :slight_smile:

I’m so glad I have found this forum.
I’ll try the following syntax.
Could you please tell me if I can make it more elegant and less hard-coded
or if you see any obvious errors:


RewriteEngine On
RewriteCompatibility2 On
RepeatLimit 32
RewriteBase 

RewriteRule ^\\/Bing$ / [NC,L,P]
RewriteRule ^\\/Zynga$ / [NC,L,P]
RewriteRule ^\\/([0-9]+)$ / [NC,L,P]
RewriteRule ^\\/CT([0-9]+)$ / [NC,L,P]

RewriteCond %{HTTP_REFERER} ^.*\\/Bing$ [NC]
#RewriteCond %{HTTP_REFERER} ^.*\\/bing$
RewriteRule ^(.*)\\/ActivitiesService.asmx\\/GetLatest$ http://$1/ActivitiesService.asmx/GetLatestSearchActivities [NC,L,P]

RewriteCond %{HTTP_REFERER} ^.*\\/Bing$ [NC]
RewriteRule ^(.*)\\/images/ConduitLive_header.png$ http://$1/images/Header_WatchItWithBing.png [NC,L,R=301]

RewriteCond %{HTTP_REFERER} ^.*\\/Zynga$ [NC]
RewriteRule ^(.*)\\/ActivitiesService.asmx\\/GetLatest$ http://$1/ActivitiesService.asmx/GetLatestZyngaActivities [NC,L,P]

RewriteCond %{HTTP_REFERER} ^.*\\/([0-9]+)
RewriteRule ^(.*)\\/ActivitiesService.asmx\\/GetLatest$ http://$1/ActivitiesService.asmx/GetLatestByActivityType?activityType=%1&type=1&timestamp=10000  [NC,L,R=301]

RewriteCond %{HTTP_REFERER} ^.*\\/CT([0-9]+) [NC]
RewriteRule ^(.*)\\/ActivitiesService.asmx\\/GetLatest$ http://$1/ActivitiesService.asmx/GetLatestByCtid?ctid="CT%1"&type=1&timestamp=10000  [NC,L,R=301]

another question:
When I type /bing I get the new image only after I press F5. That is because cache is enabled and I guess the client JS saves the image under a fixed name no matter what he got from the server.
Is there any way to overcome this from server-side?

Thanks in advance !

Sure :slight_smile:


RewriteEngine On
RewriteCompatibility2 On
RepeatLimit 32
RewriteBase 

This is all IIS specific I think, as most commands don’t mean anything to me.
I assume they’re fine (since it works) but can’t be sure.


RewriteRule ^\\/Bing$ / [NC,L,P]
RewriteRule ^\\/Zynga$ / [NC,L,P]
RewriteRule ^\\/([0-9]+)$ / [NC,L,P]
RewriteRule ^\\/CT([0-9]+)$ / [NC,L,P]

Do you have to escape slashes in IIS? i.e. \/ instead of just / ?
Looks weird. Other than that the above looks okay.


RewriteCond %{HTTP_REFERER} ^.*\\/Bing$ [NC]
#RewriteCond %{HTTP_REFERER} ^.*\\/bing$
RewriteRule ^(.*)\\/ActivitiesService.asmx\\/GetLatest$ http://$1/ActivitiesService.asmx/GetLatestSearchActivities [NC,L,P]

You can drop the second RewriteCond because you already have [NC] on the first one.
Also, instead of ^.*\\/Bing$ you can just use \\/Bing$, since that is the only part you’re interested in, and the .* at the start is just putting a strain on the regex engine for no reason.


RewriteCond %{HTTP_REFERER} ^.*\\/Bing$ [NC]
RewriteRule ^(.*)\\/images/ConduitLive_header.png$ http://$1/images/Header_WatchItWithBing.png [NC,L,R=301]

^ see above, you don’t need the ^.*


RewriteCond %{HTTP_REFERER} ^.*\\/Zynga$ [NC]
RewriteRule ^(.*)\\/ActivitiesService.asmx\\/GetLatest$ http://$1/ActivitiesService.asmx/GetLatestZyngaActivities [NC,L,P]

^ see above, you don’t need the ^.*


RewriteCond %{HTTP_REFERER} ^.*\\/([0-9]+)
RewriteRule ^(.*)\\/ActivitiesService.asmx\\/GetLatest$ http://$1/ActivitiesService.asmx/GetLatestByActivityType?activityType=%1&type=1&timestamp=10000  [NC,L,R=301]

^ see above, you don’t need the ^.*


RewriteCond %{HTTP_REFERER} ^.*\\/CT([0-9]+) [NC]
RewriteRule ^(.*)\\/ActivitiesService.asmx\\/GetLatest$ http://$1/ActivitiesService.asmx/GetLatestByCtid?ctid="CT%1"&type=1&timestamp=10000  [NC,L,R=301]

another question:
When I type /bing I get the new image only after I press F5. That is because cache is enabled and I guess the client JS saves the image under a fixed name no matter what he got from the server.
Is there any way to overcome this from server-side?

On Apache I would look at mod_expires, but on IIS I really would know, sorry.
A google for “IIS image cache control” should turn up some suggestions I’d say :slight_smile:

Weir,

trying the following:


RewriteCond %{HTTP_REFERER} /Bing$ [NC]
RewriteRule ^(.*)/ActivitiesService.asmx/GetLatest$ http://$1/ActivitiesService.asmx/GetLatestSearchActivities [NC,L,P]


I got in fiddler:



<html><body>Error in ISAPI_Rewrite helper ISAPI extension.<br>12005 - The URL is invalid
<br>File: .\\Proxy.cpp, Line: 228.<br/>Incorrect URL format: http:///ActivitiesService.asmx/GetLatestSearchActivities</body></html>

meaning - it didn’t catch anything in the (group)

And if you put the backslashes back in there?

RewriteRule ^/Bing$ http://%{HTTP:Host} [NC,L,P]
RewriteRule ^/Zynga$ http://%{HTTP:Host} [NC,L,P]
RewriteRule ^/([0-9]+)$ http://%{HTTP:Host} [NC,L,P]
RewriteRule ^/CT([0-9]+)$ http://%{HTTP:Host} [NC,L,P]

RewriteCond %{HTTP_REFERER} ^http://(.*)/Bing$ [NC]
RewriteRule /ActivitiesService.asmx/GetLatest$ [noparse]http://%1/ActivitiesService.asmx/GetLatestSearchActivities[/noparse] [NC,L,P]

RewriteCond %{HTTP_REFERER} ^http://(.*)/Bing$ [NC]
RewriteRule /images/ConduitLive_header.png$ [noparse]http://%1/images/Header_WatchItBing.png[/noparse] [NC,L,R=301]

RewriteCond %{HTTP_REFERER} ^http://(.*)/Zynga$ [NC]
RewriteRule /ActivitiesService.asmx/GetLatest$ [noparse]http://%1/ActivitiesService.asmx/GetLatestZyngaActivities[/noparse] [NC,L,P]

RewriteCond %{HTTP_REFERER} [1]http://(.*)/([/noparse][0-9]+) [NC]
RewriteRule /ActivitiesService.asmx/GetLatest$ [noparse]http://%1/ActivitiesService.asmx/GetLatestByActivityType?activityType=%2&type=1&timestamp=10000[/noparse] [NC,L,R=301]

RewriteCond %{HTTP_REFERER} [2]http://(.*)/CT([/noparse][0-9]+) [NC]
RewriteRule /ActivitiesService.asmx/GetLatest$ [noparse]http://%1/ActivitiesService.asmx/GetLatestByCtid?ctid=CT%2&type=1&timestamp=10000[/noparse] [NC,L,R=301]


  1. noparse ↩︎

  2. noparse ↩︎

Why are you posting that code? Do you have it working, or is there a problem with it? :slight_smile: