I am really new to IIS, and I am trying to make a conditional redirect if its possible.
I have built a gateway website which I have deployed on the same server as the website that I want to put a conditional redirect on; both are within the default website and run from the same URL i.e.
www.website.com/siteA or
www.website.com/siteB
What I want to do is if anyone accesses siteB, if the referer isn't siteA, then I want them to be redirected to siteA.
I have been trying to play around with the URL Rewrite but I can't seem to do this.
Just so you know, I know this is possible in code, but we don't have access to siteB's code as it is a 3rd party application. siteA I built myself and the idea is that it intercepts all requests to siteB and checks the a status field in a database to decide
whether to let the request go to siteB or whether to send the request to an information page.
You can not check the user referer without using .NET code.
You could create a new default document for site B (
how to set default page in IIS7 ) which will check the referer and to redirect to the site A or site B old default document.
I am really new to websites so can manage to make simple applications currently using the templates in vs, but how would I go about making a new default document?
Would this be deployed in my siteA application and then using your instuctions in the link above, set this as the default document in siteB?
OR
Do you some how make a new default document separately and then just copy it in to the root for siteB then follow the unstructions to set a new default document?
Finally how would your refer to the old default document from your new default document do you have any examples?
There is a problem with the resource you are looking for, and it cannot be displayed.
This is what I did but it produced the error as descriped. I put it directly in to the web.config for siteB (replacing the urls accordingly) within the <system.webServer> quotes.
I have an update I have used your example and followed the rewrite tool in IIS which built it in to the web config for me. I tested each stage as I did it as when I put it in the config manually and then went in to the rewrite tool in iis, it errored loading
the config.
Ultimate all that was missing was the <rewrite> tags everything else is identical.
So now I no longer get the error 500, but it doesn't redirect me if I access the siteB directly. What I was expecting is if I try to directly access siteB I will get redirected to siteA, or if another site reffers me that is not siteA I will get redirected
to siteA. Obviously a refferal from siteB should be allowed to go to siteA, but currently it seems everything is allowed straight to siteA?
I must be doing something wrong, do you have any ideas where I am going wrong?
Thanks again for all the help I really appreciate it.
If the referer is from siteB, it should redirect to siteA for you. To locate the root cause, more detailed trace is required. I suggest you enable failed request tracing to see what is the input of HTTP_REFERER for the request:
I have done what you have suggested and it seems that the redirect never gets instantiated, I had got it redirecting to google as a test and I also had it saying there way a loop when siteA refered to siteB but now I am just getting nothing again, i.e. it
is just going straight to siteB regardless of the referer.
It seems in the pattern match on my trace that it is only analysing the end of the URLS e.g. things like:
mcinnes01
9 Posts
Redirect if referer is not X using URL Rewrite?
May 01, 2012 01:56 PM|LINK
Hi,
I am really new to IIS, and I am trying to make a conditional redirect if its possible.
I have built a gateway website which I have deployed on the same server as the website that I want to put a conditional redirect on; both are within the default website and run from the same URL i.e. www.website.com/siteA or www.website.com/siteB
What I want to do is if anyone accesses siteB, if the referer isn't siteA, then I want them to be redirected to siteA.
I have been trying to play around with the URL Rewrite but I can't seem to do this.
Just so you know, I know this is possible in code, but we don't have access to siteB's code as it is a 3rd party application. siteA I built myself and the idea is that it intercepts all requests to siteB and checks the a status field in a database to decide whether to let the request go to siteB or whether to send the request to an information page.
Any help would be much appreciated.
Andy
HostingASPNe...
736 Posts
Re: Redirect if referer is not X using URL Rewrite?
May 01, 2012 02:24 PM|LINK
Hello,
You can not check the user referer without using .NET code.
You could create a new default document for site B ( how to set default page in IIS7 ) which will check the referer and to redirect to the site A or site B old default document.
Regards
Free ASP.NET Examples and source code.
mcinnes01
9 Posts
Re: Redirect if referer is not X using URL Rewrite?
May 02, 2012 09:24 AM|LINK
Hi,
Thanks for that I have 3 questions...
I am really new to websites so can manage to make simple applications currently using the templates in vs, but how would I go about making a new default document?
Would this be deployed in my siteA application and then using your instuctions in the link above, set this as the default document in siteB?
OR
Do you some how make a new default document separately and then just copy it in to the root for siteB then follow the unstructions to set a new default document?
Finally how would your refer to the old default document from your new default document do you have any examples?
Thanks again for your help, its much appreciated.
Andy
Lloydz
2335 Posts
Microsoft
Re: Redirect if referer is not X using URL Rewrite?
May 04, 2012 02:36 AM|LINK
Hi,
In rewrite module, we can add conditions to implement the rediection by evaluating http_referer header. The sample rule looks like:
<rules>
<rule name="RedirectToSiteA" stopProcessing="true">
<match url="^SiteB(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="http://www.website.com/SiteA(.*)" negate="true" />
</conditions>
<action type="Redirect" url="http://www.website.com/siteA{R:1}" />
</rule>
</rules>
Feel free to let me know if you still have trouble for this task, thanks.
mcinnes01
9 Posts
Re: Redirect if referer is not X using URL Rewrite?
May 09, 2012 08:54 AM|LINK
Hi,
Sorry for the newb questions but do I put this in the web config for siteB?
Thanks again,
Andy
Lloydz
2335 Posts
Microsoft
Re: Redirect if referer is not X using URL Rewrite?
May 09, 2012 09:20 AM|LINK
You are correct. The settings should be put in site web.config. Please let me know if this work for you.
Thanks.
mcinnes01
9 Posts
Re: Redirect if referer is not X using URL Rewrite?
May 15, 2012 09:42 AM|LINK
Hi Lloyd,
I just got a 500 error.
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
This is what I did but it produced the error as descriped. I put it directly in to the web.config for siteB (replacing the urls accordingly) within the <system.webServer> quotes.
<system.webServer>
<rules>
<rule name="RedirectToSiteA" stopProcessing="true">
<match url="^www.website.com/SiteB(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="http://www.website.com/SiteA(.*)" negate="true" />
</conditions>
<action type="Redirect" url="http://www.website.com/siteA{R:1}" />
</rule>
</rules>
<system.webServer>
Is there something I am doing wrong? I also just tried <match url="^SiteB(.*)" stopProcessing="true"> but that did the same thing.
There isn't anything I need to do in IIS as well is there?
Thanks again
Andy
mcinnes01
9 Posts
Re: Redirect if referer is not X using URL Rewrite?
May 15, 2012 10:21 AM|LINK
Hi
I have an update I have used your example and followed the rewrite tool in IIS which built it in to the web config for me. I tested each stage as I did it as when I put it in the config manually and then went in to the rewrite tool in iis, it errored loading the config.
Ultimate all that was missing was the <rewrite> tags everything else is identical.
<rewrite>
<rules>
<rule name="RedirectToSiteB" stopProcessing="true">
<match url="siteB" />
<conditions>
<add input="{HTTP_REFERER}" pattern="(.*)www.website.com/siteA(.*)" negate="true" />
</conditions>
<action type="Redirect" url="http://www.website.com/siteA" />
</rule>
</rules>
</rewrite>
So now I no longer get the error 500, but it doesn't redirect me if I access the siteB directly. What I was expecting is if I try to directly access siteB I will get redirected to siteA, or if another site reffers me that is not siteA I will get redirected to siteA. Obviously a refferal from siteB should be allowed to go to siteA, but currently it seems everything is allowed straight to siteA?
I must be doing something wrong, do you have any ideas where I am going wrong?
Thanks again for all the help I really appreciate it.
Andy
Lloydz
2335 Posts
Microsoft
Re: Redirect if referer is not X using URL Rewrite?
May 16, 2012 03:21 AM|LINK
If the referer is from siteB, it should redirect to siteA for you. To locate the root cause, more detailed trace is required. I suggest you enable failed request tracing to see what is the input of HTTP_REFERER for the request:
Using Failed Request Tracing to Trace Rewrite Rules
http://learn.iis.net/page.aspx/467/using-failed-request-tracing-to-trace-rewrite-rules/
Thanks.
mcinnes01
9 Posts
Re: Redirect if referer is not X using URL Rewrite?
May 16, 2012 10:08 AM|LINK
Hi,
I have done what you have suggested and it seems that the redirect never gets instantiated, I had got it redirecting to google as a test and I also had it saying there way a loop when siteA refered to siteB but now I am just getting nothing again, i.e. it is just going straight to siteB regardless of the referer.
It seems in the pattern match on my trace that it is only analysing the end of the URLS e.g. things like:
login.aspx, WebResource.axd etc
Here is an example:
7. -URL_REWRITE_START
RequestURL
/SiteB/WebResource.axd?d=bzb5XAS-T8BPUEXyS2UPYjui7_-tjeTJdmARQXGNISCNtworBbU40i5oI2ygYLA5_xyUnPX1qgqUSM82dEhHQrRO0kw1&t=634245190248165742&c=1
Scope
1
Type
0
Scope
Distributed
Type
Inbound
0 ms
Verbose8. -RULE_EVALUATION_START
RuleName
GateKeeper
RequestURL
WebResource.axd
QueryString
d=bzb5XAS-T8BPUEXyS2UPYjui7_-tjeTJdmARQXGNISCNtworBbU40i5oI2ygYLA5_xyUnPX1qgqUSM82dEhHQrRO0kw1&t=634245190248165742&c=1
PatternSyntax
0
StopProcessing
true
RelativePath
/siteB/
PatternSyntax
Regex
Verbose9. -PATTERN_MATCH
Pattern
siteB(.*)
Input
WebResource.axd
Negate
false
Matched
false
0 ms
Verbose10. -RULE_EVALUATION_END
RuleName
GateKeeper
RequestURL
siteB/WebResource.axd
QueryString
?d=bzb5XAS-T8BPUEXyS2UPYjui7_-tjeTJdmARQXGNISCNtworBbU40i5oI2ygYLA5_xyUnPX1qgqUSM82dEhHQrRO0kw1&t=634245190248165742&c=1
StopProcessing
true
Succeeded
false
0 ms
Informational11. -URL_REWRITE_END
RequestURL
This is my current config:
<rewrite>
<rules>
<rule name="GateKeeper" patternSyntax="ECMAScript" stopProcessing="true">
<match url="siteB(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_REFERER}" pattern="siteA(.*)" negate="true" />
</conditions>
<action type="Redirect" url="http://www.website.com/siteA" />
</rule>
</rules>
</rewrite>