How to check condition in url rewrite - asp.net

I have a rule in url rewrite module like below
<rule name="articleSectionUrl">
<match url="(.+)/([0-9]+)/(.+)/([0-9]+)" />
<action type="Rewrite" url="article.aspx?articleid={R:4}" appendQueryString="false" />
</rule>
It works properly as it detects below type of article url's:
http://www.emusician.com/news/0766/spl-launches-usb-and-madi-interfaces/151461
That's fine, but it also detects below type of url, which is the image path unfortunately:
http://www.emusician.com/Portals/9/SlideShowThumbnails/15/ad300x250_02.gif
Can anyone tell me how can i avoid this.

Thanks all, #Aquillo as per your regex it will just limit the file whose extension I provide in the regex, that means I always have to provide the extensions in the regex for the url I want to ignore, so here's the complete solution to my problem.
UrlRewrite module has a <conditions> element under <rule> with this we limit any type of file which comes in the url.
Here's the complete solution:
<rule name="articleSectionUrl">
<match url="(.+)/([0-9]+)/(.+)/([0-9]+)" />
<action type="Rewrite" url="article.aspx?articleid={R:4}" appendQueryString="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
</rule>
Thanks guys for helping me.

Related

IIS rewrite URL in asp.net goes to into infinity loop

I just need to create a rule in my web config file to rewrite all URLs to given into rule
ex:
url/services/presentations-evenementielles to
url/Services/Presentations-Evenementielles
This is all made for SEO purposes to avoid duplicates.
<rule name="ProductionRule2" stopProcessing="true" patternSyntax="ExactMatch" >
<match url="^/productions/xyz" ignoreCase="true" />
<action type="Redirect" url="Productions/XYZ" redirectType="Permanent"/>
</rule>
Above code its gives me infinite loop error.
There was some mistakes with your rule
1.You were trying to use "^" in an exact match rule. Please set it to regular expression
2.You are trying to use "/" in match url. The match url part of domain/productions/xyz is
productions/xyz instead of /production/xyz.
3.You are enabling ignore case when you redirect URL to itself.
So please try to modify the rule like this
<rule name="ProductionRule2" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^productions/xyz" ignoreCase="false" />
<action type="Redirect" url="Productions/XYZ" redirectType="Permanent" />
</rule>
Update:
Please modify the rule like this
<rule name="ProductionRule2" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^Productions/XYZ" ignoreCase="false" negate="true" />
<action type="Redirect" url="Productions/XYZ" redirectType="Permanent" />
<conditions>
<add input="{ToLower:{URL}}" pattern="/productions/xyz" />
</conditions>
</rule>
And this is working on my side
Please remember to clean browser cache when you test the rule.And the URL should be prODuctions/Xyz instead of prODuction/Xyz.

Combine several 301 redirects into one for ASP.NET URL Rewrite module

I am trying to move a complex redirect logic implemented in Global.asax into a set of rules for the IIS URL Rewrite module in a classic ASP.NET website. Shortly, the logic must redirect requests like
{http|https}://[www.]ourdomain.com/[Home/]Products/old-product-name/page.aspx
to
https://ourdomain.com/new-product-name/
(optional and variable parts are in square and curly brackets).
The first 3 main things I implemented with URL Rewrite rules are the followings:
<rule name="Redirect to HTTPS">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host Name">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
</conditions>
<action type="Redirect" url="https://ourdomain.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Default Page">
<match url="(.*)default.aspx" />
<conditions>
<add input="{REQUEST_URI}" negate="true" pattern="-default.aspx$" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
These rules allow to redirect requests like http://www.ourdomain.com/product-name/default.aspx to https://ourdomain.com/product-name.
However, the browser developer tools report that this conversion causes 301 redirects. I tried to find a solution of this redirect chain problem in the Internet and found this post. After reading it, I managed to recode my rules to have just one final 301 redirect with URL rewriting:
<rule name="Redirect to HTTPS">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="_{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host Name">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
</conditions>
<action type="Rewrite" url="_{R:1}" redirectType="Permanent" />
</rule>
<rule name="Default Page">
<match url="(.*)default.aspx" />
<conditions>
<add input="{REQUEST_URI}" negate="true" pattern="-default.aspx$" />
</conditions>
<action type="Rewrite" url="_{R:1}" redirectType="Permanent" />
</rule>
<rule name="FINAL REDIRECT" stopProcessing="true">
<match url="^(_+)(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_METHOD}" pattern="GET" />
</conditions>
<action type="Redirect" url="https://ourdomain.com{R:2}" />
</rule>
However, this approach looks non-natural. It makes hard adding new rules because of these workarounds with the underscore character. And maybe, such several rewrite operations for one request may impact on the website performance.
Is there another, more elegant solution, of the 301 redirect chain problem for the URL Rewrite module?
This is not a limitation of IIS Url Rewrite, but the way the web works. A redirection sends a header to the browser telling it that it should navigate to another place. If you have 3 rules that apply to the same original request, and the 3 apply a redirection, then you'll always get 3 redirections no matter how you do it: IIS Url Rewrite, Apache .htaccess or your own custom code.
The solution you've implemented is a "patch" to change several redirections into internal rewrites in the server and a final "catch" of those special rewrites to transform them into a unique redirection at the end (BTW, if you have any real page that strats with _ you won't be able to access it with this extra rule). That's not bad and won't affect your performance, but it's ugly.
I'm not a SEO expert but I think the 301 chaining thing to reach a final similar URL won't hurt your SEO at all in recent times, if that's what worries you (read the final part specially). And being 301 redirects they will affect only the first visit of your users, so probably won't hurt your loading times or conversions either anyway.
However, if you want to avoid that, try to make a single rule for your purpose, doing all the transformations at once. In your case, you want to transform this:
{http|https}://[www.]ourdomain.com/[Home/]Products/old-product-name/page.aspx
(I guess /Home/ can be present sometimes or not)
into this:
https://ourdomain.com/new-product-name/
Then you can use this single rule:
<rule name="New URLs!!" stopProcessing="true">
<match url="^(Home/){0,1}Products/(.+?/).+.aspx" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
</conditions>
<action type="Redirect" url="https://ourdomain.com/{R:2}" redirectType="Permanent" />
</rule>
and you will transform everything with a single rule (so, just one redirect). The regular expression in the match URL part will identify this specific kind of URL (old product URLs) and the conditions (matching any of them) will take care of the HTTPs and domain change. The stopProcessing="true" will keep other rules to act, and you'll have this specific kind of URLs under control.
Please note that I've not tested this rule and maybe it has minor problems. But you get the idea...
You'll need extra rules after this one to take care of other less-specific URLs, such as your general HTTP to HTTPS or canonical domain rules. But in the case you want to resolve, this will achieve what you want in a single step.

IIS Rewriting with Query String and Question Mark IIS 7

I have read the threads and I still can't work it out, my problem I know, but wondering if could point me in the right direction.
I have setup a rule:
www.mydomain.com/productlist.asp?CategoryID=2
rewritten to
www.mydomain.com/homeware/cushions
.
<rule name="cushions">
<match url="^homeware/cushions" />
<action type="Rewrite" url="productlist.asp?CategoryID=2" />
</rule>
ok, now my problem is with pagination of the results, so the original domain when the user goes onto the next page would look like:
www.mydomain.com/productlist.asp?CategoryID=2&Page=2
This is where I'm having problems - I want this to become:
www.mydomain.com/homeware/cushions?page=2
and ongoing for how many pages
just can't get it to work - I understand I need to use query string but really struggling. Asking for expertise, thanks for any help
To capture the Query String you need to use conditions for that since the match URL does not include it.
So you could do that with a rule like:
<rule name="cushions" stopProcessing="true">
<match url="^homeware/cushions$" />
<conditions>
<add input="{QUERY_STRING}" pattern="page=(\d+)" />
</conditions>
<action type="Rewrite" url="productlist.asp?CategoryID=2&page={C:1}" appendQueryString="false" />
Just add appendQueryString="true" in action will automatically append query string.
<rewrite>
<rules>
<rule name="test">
<match url="^homeware/cushions" />
<action type="Rewrite" url="/test/test.cfm?category=5" appendQueryString="true" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>
http://localhost:8081/homeware/cushions?page=2
You will receive page in URL variable with value 2.

IIS URL Rewrite Module : Rewrite Appends QueryString

I have the following rewrite rule.
<rule name="cheapbastardz.signupcode" stopProcessing="true">
<match url="^signup/code/([_0-9a-z-%=\+\$]*)$" />
<conditions>
</conditions>
<action type="Rewrite" url="Page.aspx?sc={R:1}" appendQueryString="false" />
</rule>
Now when the user click on a buton where the page is posted back the url changes from
http://localhost/CBAanmelding/signup/code/3f69fa28-5c6c-4815-a2b3-3c846651bed9
to
http://localhost/CBAanmelding/signup/code/3f69fa28-5c6c-4815-a2b3-3c846651bed9?sc=3f69fa28-5c6c-4815-a2b3-3c846651bed9
I don't want the querystring to appear. How can I prevent this. Thanks in advance.
Gr
Martijn
Are you sure your rule is firing?
The match pattern does not seem to fit the incoming URL. The incoming URL begins with /CBAanmelding, while your pattern looks for URLs that bbegin with signup. The circumflex preceding the word signup is a zero-width assertion that the pattern matches only at the beginning of the URL.
Maybe you need to change the pattern?
<rule name="cheapbastardz.signupcode" stopProcessing="true">
<match url="^CBAanmelding/signup/code/([_0-9a-z-%=\+\$]*)$" />
<conditions>
</conditions>
<action type="Rewrite" url="Page.aspx?sc={R:1}" appendQueryString="false" />
</rule>
use
form1.Action = Request.RawUrl;

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