How to do 1 URL re-write rule for multiple ids? - asp.net

I have these URLs:
https://www.example.com/amp/articles/10/title
https://www.example.com/amp/videos/11/title
I need to redirect only specific ids to non amp as follows:
https://www.example.com/articles/10/title
https://www.example.com/videos/11/title
Here's what I tried but there's no URL redirection, nothing occurs:
<rule name="redirect_to_non_amp_for_specific_ids" stopProcessing="true">
<match url="^amp/(articles|videos)/(10|11)/([^/]+)/?$"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}/{R:3}"/>
</rule>
Note I need to avoid multiple url-rewrite rules.

My rule is correct. I figured out I had to place it before other article rules, for priority, for the rule to take effect.
From microsoft: The rules are evaluated in the same order in which they are specified.
Reference: https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

Related

IIS manager url rules for similar urls

I have two urls: http://...../m?PageView=View1&Language=English&AName=AAA and another http://...../m?PageView=View2TName=T1&AName=XYZ. Both this urls are for separate section/functionality. But as the number and pattern of parameters are same one url work and another does not.
I want to write url redirect and rewrite rules for two similar urls. I have written first rule as below.
<rule name="RedirectUserFriendlyURL12" stopProcessing="true">
<match url="^m/$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^View=([^=&]+)&Language=([^=&]+)&AName=([^=&]+)$" />
</conditions>
<action type="Redirect" url="m/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL12" stopProcessing="true">
<match url="^m/([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="m?View={R:1}&Language={R:2}&AName={R:3}" />
</rule>
and another url has same number of parameters but different name as below. Here is 2nd rule.
<rule name="RedirectUserFriendlyURL12" stopProcessing="true">
<match url="^m/$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^View=([^=&]+)&TName=([^=&]+)&AName=([^=&]+)$" />
</conditions>
<action type="Redirect" url="m/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL12" stopProcessing="true">
<match url="^m/([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="m?View={R:1}&TName={R:2}&AName={R:3}" />
</rule>
When I have above two rules in web.config, one url works properly i.e. rediected and rewritten But another one does not work.
How can I differentiate both the rules so it works for both the urls.
I solved my issue. I have kept only one rule only, first one.
But in my controller code actually I had to map parameters accordingly. Means not TName parameter value I have to access, I have to access language parameter only for View-2 also, as value of TName is getting passed in Language parameter when rules get apply.
I could have use two rules but then I would have to change the redirect target URL. Like redirect
^View=([^=&]+)&TName=([^=&]+)&AName=([^=&]+)$ to m/{C:1}/tname/{C:2}/{C:3}
Then rewrite back from
^m/([^/]+)/tname/([^/]+)/([^/]+)/?$ to m?View={R:1}&TName={R:2}&AName={R:3}.
But I didn't wanted to have this above thing.

IIS rewrite - add '.html' to any URL's that don't have an extension

I'm trying to setup an IIS rewrite rule to add '.html' to URL's that don't have an extension, eg:
Original URL: www.domain.com/page
Rewrite to: www.domain.com/page.html
And I want to ignore any URL's that have an extension (eg. if they are images or other files)
Does anybody know the rule that I would need to set this up?
I worked this out myself by modifying a rule that I had on another website:
<rule name="rewrite directories to html" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.[a-zA-Z]{1,4}$" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.html" />
</rule>

Remove ASPX FILE Extention in Web.config But Negate Rewrite if URL Contains Period

All right, so by now most of us probably use the standard rule below for remove the aspx extention in your urls.
<rule name="Remove">
<!--Removes the .aspx extension for all pages.-->
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
However I would like to modify the rule to prevent the write rule from catching any url with a period in it.
That way if someone tries to type in http://www.domain.com/picture.jpg
The rule doesn't catch it.
Luckily the IsFile and IsDirectory conditions prevent actual files from being hit by the rule but whenever I have a case where someone types in a file that doesn't exist on the server then the rule catches it and asp.net does something like this:
http://www.domain.com/404error.aspx?aspxerrorpath=/picture.jpg.aspx
Id like it to not pass through the rule when there is a file not found.
Basically I just need to be able to add a condition that negates whenever a period is found in the url after the domain name. I'm assuming some sort of REGEX will work. I can't seem to get it working right though.
I was able to come up with the following solution.
<rule name="RewriteASPX" stopProcessing="true" enabled="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.aspx" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>

In the IIS7 URL Rewrite module, can I specify in a redirect rule to not apply to http-post requests?

In IIS7 URL Rewrite module, can I specify in a redirect rule to not apply to http-post requests? I am using the templates provided by Microsoft to lowercase all urls and to append a trailing slash. However I have a AJAX post requests that don't meet this specification but they break we they are rewritten as 301s. I am not worried about POST requests for SEO so I would prefer if I could just specify in the rule to ignore it. Below are my rules:
<rule name="AddTrailingSlashRule" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>
<rule name="LowerCaseRule" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>
You have access to that in the {REQUEST_METHOD} variable under the conditions.
<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" />
We've had the same problem as the OP a while back, and then applied patridge's solution, which worked fine until we noticed some REST DELETE calls would fail. Turned out to be the trailing slash redirect making GETs out of the DELETE requests.
So I modified the solution to make the redirect rule to apply only to GET requests.
<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="GET" ignoreCase="true" />

IIS7 URL Rewrite Mod causes unwanted effects

This might be more a a regex question, but it is holding up our release. I'm unable to come up with a clever solution.
Basically, we want to rewrite www.oursite.com/Games.aspx?g=classic&m=etc to www.oursite.com/classic/?m=etc
The problem is that the rule itself (at least as generated by the URL Rewrite mod for IIS7) looks like this:
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^Games\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />
<add input="{QUERY_STRING}" pattern="^g=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Games.aspx?g={R:1}" />
</rule>
</rules>
</rewrite>
And thus, any other file that matches a similar pattern, for example Item/?id=23 is being rewritten. In addition, our script resource file is being rewritten, so the whole site throws 30 javascript errors. According to our specs, it's unacceptable in our specs to have the url www.oursite.com/Games/classic OR www.oursite.com/g/classic. Any solution? Manythanks!
Perhaps instead of matching ([^/]+)/?$ in your second rule, you'd be better of explicitly stating which things you want to rewrite, in a regex OR statement:
(classic|somethingelse|athirdsomething)/?$
That way only the items you explicitly want to rewrite will be modified. I don't know how many of these you're potentially needed to rewrite, so if it's a large number, this might not be viable, but if it's only a few categories, then it's probably the most straightforward.
Alternatively, if there are only certain prefixes that shouldn't be rewritten, you could simply add negated conditions that pattern match those prefixes to your rewrite.

Resources