What I am trying to achieve is following, grab the number from the query parameter and see if it exists in referrer header.
What I've got so far (clearly doesn't work):
<rule name="Epic rule in progress, maybe." stopProcessing="true">
<match url="^my/path/goes/here" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="^foo=.*-([0-9]+)$" />
<add input="{HTTP_REFERER}" pattern=".*{R:1}.*" negate="true" />
</conditions>
<action type="CustomResponse" statusCode="666" statusReason="IDK yet." statusDescription="The end is near." />
</rule>
Similar bit slightly different approach (not entirely sure I can fetch query params from match URL):
<rule name="Epic rule in progress, maybe." stopProcessing="true">
<match url="^my\/path\/goes\/here\?foo=([0-9])" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_REFERER}" pattern=".*{R:1}.*" negate="true" />
</conditions>
<action type="CustomResponse" statusCode="666" statusReason="IDK yet." statusDescription="The end is near." />
</rule>
Example scenario:
user lands on page "/mypage-123"
JS on the page triggers a request to "my/path/goes/here?foo=123" (referrer is "/mypage-123")
If "123" does not exist in referer, throw status code 666.
The question is:
Is it actually possible to achieve what I am trying above?
Where am I going wrong?
Related
I need to write a redirect to strip off parameters when a certain one exists, but I would like the redirect to be the same as the incoming url, just without the parameters. This is what I had and it doesn't work. I thought if I put the request URI in there without appending the querystring, then it would work but it results in a loop. Has anyone done this before?
<rule name="Remove parameters" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="page=([0-9]+)" />
</conditions>
<action type="Redirect" url="{REQUEST_URI}" appendQueryString="false" />
The problem with jallen's answer is that the whole query string will be removed, which may not be ideal if you want to maintain certain parts of it. An alternative would be as follows:
<rule name="Remove paging parameters" stopProcessing="true">
<match url="(.*)?$" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="(.*)(page=.+)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}{C:3}" appendQueryString="false" />
</rule>
C:1 = Everything before the matching page parameter
C:2 = Is the
match itself, and what we want to exclude
C:3 = Everything after the
matching page parameter
Hence, we use the redirect {C:1}{C:3} to exclude only the page query string.
Got it with the following:
<rule name="Remove paging parameters" stopProcessing="true">
<match url="(.*)?$" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="page=([0-9]+)" />
</conditions>
<action type="Redirect" url="{R:1}" appendQueryString="false" />
We are trying to use Imageresizer with the disk cache feature as well as the sqldatareader. It expects urls to be in the form of:
http://somesite.com/image/{imageid}.{extension}
whereas all the image links in our site is currently:
http://somesite.com/image.aspx?imageid={imageid}&format={extension}
The best solution I have found so far to convert these is UrlRewrite but we are kind of doing the opposite of what it intends (taking nice urls to nasty). I have been struggling to get the rewrite rule correct for this and was hoping that somebody could help. Below is what I currently have and am aware it may be completely wrong:
<rewrite>
<rules>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^image.aspx?([^imageid=]+)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="false" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="image/{R:1}.jpg" />
</rule>
</rules>
</rewrite>
Was able to get the basic functionality to work with the following rule.
<rule name="Redirect Category Name and Sort By" stopProcessing="true">
<match url="^image\.aspx$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="^imageid=([0-9]+)" />
</conditions>
<action type="Rewrite" url="image/{C:1}.jpg" appendQueryString="true" />
</rule>
How to redirect all users to http://redirect.url except "GoogleBot"
<rule name="nonbot" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern=".+Bot.+" negate="true" />
</conditions>
<action type="Redirect" url="http://redirect.url" appendQueryString="false" redirectType="Found" />
</rule>
don't work.
As seen in the comments, the problem had to do with case.
Googlebot's name and useragent string both have the b in lowercase. The filter in the code looks for GoogleBot. Even though the OP set the filter to ignore case, it somehow didn't. So there.
I've got IIS7.5 running, and I'm trying to match an explicit URL. The following code does not work.
<rule name="presid" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" matchType="Pattern" pattern="http://www.example.com/presid" ignoreCase="true" negate="false" />
</conditions>
<action type="Redirect" url="http://www.example.net/presid" />
</rule>
I've also tried escaping the periods:
http://www\.example\.com/presid
but that doesn't seem to work either. How do I match a specific full URL using IIS7.5?
This works, but does not do exactly what I'm after: (.*)/presid, but I have images that break:
http://example.com/images/presid/eee.jpg
I don't want that image to get redirected.
REQUEST_URI does not contain the hostname.
use this instead:
<add input="{REQUEST_URI}" matchType="Pattern" pattern="^/presid$" ignoreCase="true" negate="false" />
When I needed to match an exact full URL, I have used 2 conditions, one for the host and one for the rest, such as:
<add input="{HTTP_HOST}" pattern="www.yourdomain.com" />
<add input="{REQUEST_URI}" pattern="pages/yourpage.aspx" />
I have the attached redirect and rewrite rules. Essentially, www.foo.com/vehicles/vehicle-detail.aspx?stockno=123456 is rewritten to www.foo.com/123456. This works as expected with the rules I have in place. Now, if I attempt to browse to a page that does not exist (i.e. www.foo.com/test.aspx) the rewrite rule qualifies and IIS attempts to forward the request to www.foo.com/vehicles/vehicle-detail.aspx (without the querystring). On the vehicle-detail.aspx page I have code to redirect to the homepage if there isnt a valid stockno in the querystring, so this is what is happening. I am not sure how to correct the rule(s), so that the condition does not qualify as true. I have attached rules and screenshots of Failed Request Tracing I enabled to watch the request/rewrite rule.
<rule name="Redirect StockNo" enabled="true" stopProcessing="true">
<match url="^Vehicles/Vehicle-Detail\.aspx$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^stockno=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
<rule name="Rewrite StockNo" enabled="true" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Vehicles/Vehicle-Detail.aspx?stockno={R:1}" />
What you need is something more unique for the friendly pattern. If you know that it's always going to be digits, you can have:
<match url="^([\d]+)/?$" />
This will only match to www.foo.com/{digits} with an optional trailing slash.
Another option is to make the url something like www.foo.com/s/{stockno}/. With the /s/ you have something unique that you can confidently match. S is just an example that is short for stockno, so you can use what you want.