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" />
Related
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?
i have +10 domains on one solution, and about half of them have had an SSL certificate attached. I seem to have trouble creating ONE rule to make the correct ones being forced onto https.
I can to a rule for each of them, but feel this should have worked:
<rule name="HTTP to HTTPS redirect 1" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.domainNo1.dk$" />
<add input="{HTTP_HOST}" pattern="^www.domainNo2.dk$" />
<add input="{HTTP_HOST}" pattern="^www.domainNo3.dk$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
this leaving the domain4, domain5, etc. alone.
Unfortunately it does not kick in when I have more than one domain in the rule. I'm guessing it is the logical grouping that is probably default:
logicalGrouping="MatchAll"
But setting:
logicalGrouping="MatchAny"
will make the sites not work at all. After the redirect to https it continues to redirect the pages, resulting in "ERR_TOO_MANY_REDIRECTS".
I would have thought this was handled by
<add input="{HTTPS}" pattern="off" />
But no.
Hope anyone can help.
You could use below rewrite rule to match multiple domains:
<rule name="http to https with multiple domain name" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="www.sample1.com|www.sample3.com" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
I have one url which having ./ [period & slash] at the end of parameter. I want to redirect that url with different location but its not even detecting in rules. I am using IIS. I want to configure this on web.config
http://somesitename.com/mypage/teachers-manual./sku/8772
needs to redirect on
http://somesitename.com/mypage/teachers-manual/sku/8772
Though I have tried solution given on Here but its not even working. But if I use same thing instead of Redirect with Rewrite then Rule start working. Not sure why its not working for "Redirect".
<rule name="Trailing Dots and spaces" stopProcessing="true">
<match url="^mypage\/(.*)([\.\s]+)\/(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="/index.cfm/{R:1}/{R:2}/{R:4}" appendQueryString="true" />
</rule>
Actually when I tried to write rule then url which having ./ is also not working.[ http://somesitename.com/mypage/teachers-manual./sku/8772 ]
<rule name="Trailing Dots and spaces1.1" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://somesitename.com/newpage.html" />
</rule>
Not sure where its wrong.
Just got more information on Post & Haacked Said for it.
so I have modified file as follows and now its perfectly working for me.
<configuration>
<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="Trailing Dots and spaces1.1" stopProcessing="true">
<match url="^(.*)/(.*)\.\/(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="/{R:1}/{R:2}/{R:3}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
.... etc
I have several pages on my site that have long and unreadable URLs that I'd like to be able shorten. The problem I'm having is appending query strings with variable values.
For example:
"www.example.com/dir1/dir2/filename.php" shortens to "www.example.com/file".
"www.example.com/dir1/dir2/filename.php?id=2" would be "www.example.com/file/2".
"www.example.com/dir1/dir2/filename.php?id=2&alt=6" would be "www.example.com/file/2/6".
The values of 'id' and 'alt' are then used by our page to access information in our database, which determines the contents of the page. These values can change, and there is no set amount.
Right now I've got the first example working fine using the following rewrite rules:
<rewrite>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
</outboundRules>
<rewriteMaps>
<rewriteMap name="StaticRewrites">
<add key="/file" value="/dir1/dir2/filename.php" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite Rule">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
</rules>
</rewrite>
But I haven't been able to find anything that would allow the URLs to contain variables. Everything I've seen has used static rewrites like my current solution is using, and I can't find anything about allowing arbitrary parameters.
EDIT:
Found a better solution that doesn't use a Rewrite Map. I had attempted a simliar rule previously, but due to the IIS setup on our testing environment it wasn't working as expected. This version should work for most people.
<rule name="Curricula View" stopProcessing="true">
<match url="/file(?:/(\d+)(?:/(\d+))?)?" />
<action type="Rewrite" url="/dir1/dir2/filename.php?id={R:1}&alt={R:2}" appendQueryString="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
</rule>
Using this rule I was able to avoid using a rewrite map altogether. I had attempted to use something like this originally, but due to the setup of our test environment it wouldn't work without some weird tweaks. This version should work in all normal environments.
<rule name="Curricula View" stopProcessing="true">
<match url="/file(?:/(\d+)(?:/(\d+))?)?" />
<action type="Rewrite" url="/dir1/dir2/filename.php?id={R:1}&alt={R:2}" appendQueryString="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
</rule>
EDIT:
I was also able to get the original version with a rewrite map working for anyone interested.
<rule name="Rewrite Map with Variables" enabled="true">
<match url="^(.+?)/?/(.*)$" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{ProductMap:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="/dir1/dir2/filename.php?id={C:0}&other={R:2}" appendQueryString="true" />
</rule>
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.