ASP.NET redirect. Possible loop? - asp.net

I want to redirect from all url that contains 'flashmediaelement.swf' to '/mediaplayer/flashmediaelement.swf'. Is it possible that there will be a loop and it will be redirecting without end?
I would use
<rule name="Redirect flashmediaelement" enabled="true" stopProcessing="true">
<match url="/flashmediaelement.swf$" />
<action type="Redirect" url="/mediaplayer/flashmediaelement.swf" />
</rule>
I want to redirect from any pages ending with "/flashmediaelement.swf" (like "mypage.com/blabla/blabla/flashmediaelement.swf" or "mypage.com/flashmediaelement.swf") to "mypage.com/mediaplayer/flashmediaelement.swf"

No , your regular expression ^/flashmediaelement.swf$ specifies exact match for url , so it won't match '/mediaplayer/flashmediaelement.swf
See first section here for exact match regex

Related

Url Rewrite Regex to Match Some Classic ASP Files (But not all)

I have a bunch of old .asp files that I'm trying to redirect. I do not want to redirect all .asp files, just ones related to the "News" section.
Examples:
news_1-4-2009.asp
news_2-15-2008.asp
news.asp
I created a redirect rule that almost works:
<rule name="NewsRedirect" stopProcessing="true">
<match url=".*news(.*asp)?" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" appendQueryString="false" url="http://www.example.com/about/newsroom" redirectType="Permanent" />
</rule>
The problem (as you might see) is that the action url is also a match and creates an infinite loop.
Question
I'm trying to say...
Any file that starts with "news" and ends in ".asp".
Thanks for any help!
Any file that starts with "news" and ends in ".asp".
Give this a try:
^news.*\.asp$

IIS Redirect Regex doesn't work with a space - %20 in URL

So I have one simple problem but somehow doesnt seem to work. I have one URL http://www.domain.com/%20#axzz2ZX4J0KAS which I want to redirect to http://www.domain.com/page-name.htm. I have tried so many combinations in IIS URL Rewrite/web.config and they all seem to work inside test pattern dialog but none works in browsers.
1.
<rule name="Redirect%20InHomePage" enabled="true" stopProcessing="true">
<match url="^(.+)domain\.com/(\s|%20)(.+)" ignoreCase="true" />
<action type="Redirect" url="http://www.domain.com/page-name.htm" />
</rule>
2.
<match url="(.+)/%20(.+)" ignoreCase="true" />
3.
<match url="(.+)domain.com/ (.+)" ignoreCase="true" />
4.
<match url="(.+)domain.com/(\s|%20)(.+)" ignoreCase="true" />
As you can see I tried all of above patterns, they all work fine in Test Pattern dialog but when i browse URL, it always converts %20 to space and rule doesn't work for redirect.
Please help me for this simple yet unsolved problem, if anyone knows what am I missing.
I was having a similar issue and got it to work by typing spaces " " instead of %20 for my rules.
So here you may want to try [ ] for your space.
http://imgur.com/6sjWVjL
Don't include the domain name in your match url.
If you want to handle either having the tracking code or not in your url then you probably want to use something like this:
<rule name="RedirectSpaceInHomePage" stopProcessing="true">
<match url="^\s(#\.*)?$" />
<action type="Redirect" url="page-name.htm" />
</rule>

IIS7 url rewrite issue when having "?"

I have a URL in the format
abc/pqr/xyz/?word1
and this needs to redirect to
abc/pqr/xyz/?word2
Is is possible to do using IIS7 rewrite?
It is possible using the IIS7 rewrite module with the following rule:
<rule name="Rewrite querystring" stopProcessing="true">
<match url="^abc/pqr/xyz/?$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^word1$" />
</conditions>
<action type="Redirect" url="{R:0}?word2" appendQueryString="false" />
</rule>
What it does is: check that the url is abc/pqr/xyz/ or abc/pqr/xyz and the query string exactly word1. If yes, it redirects the user to the same url ({R:0}) but appending ?word2 instead.
It is important to have the appendQueryString="false" option as you don't want the module to append your word1 at the end.
By default, if not specified, the redirect is a 301 (permanent), that, regarding #Owen comment, seems to be the best fit for your case!
See here
You need to specify the url to match, " abc/pqr/xyz", then the query string to match and replace.

Simple trailing slash URL rewrite

I've never done URL rewriting (redirecting). I have a website http://sub.sub.domain.ext/app/. "app" signifies an "Application", not a virtual directory.
When a user navigates to http://sub.sub.domain.ext/app (no slash) I need the IIS 7 to redirect him to the URL with the trailing slash.
The thing is that I want the rule to be applied only when the user navigates to application. I don't want the trailing slash to be appended to every filename.
I have tried modifying the predefined rule in IIS7 manager but with no success. I've tried exact matching the whole URL, constraining the conditions, or simply using the original predefined rule. But even when using the original rule, it rewrites all subsequent request files/dirs/URLs, but it does not redirect the user from http://sub.sub.domain.ext/app to http://sub.sub.domain.ext/app/.
The rule you are looking might be as simple as:
<rule name="Add trailing slash" stopProcessing="true">
<match url="^app$" negate="false" />
<action type="Redirect" url="{R:0}/" />
</rule>
The url="^app$" pattern matches only the url http://www.yourwebsite.com/app and nothing else.
If you need to limit this behavior to the host sub.sub.domain.ext, you can add a condition:
<rule name="test" stopProcessing="true">
<match url="^app$" negate="false" />
<action type="Redirect" url="{R:0}/" />
<conditions>
<add input="{HTTP_HOST}" pattern="^sub.sub.domain.ext$" />
</conditions>
</rule>

calling urlrewrite from code

I have a url as a string and I want to call the asp.net code (IIS7 module) that rewrites urls on their way into my application and get a rewritten url from it:
http://webserver/nice/url/youHaveThere
to
http://webserver/app/Default.aspx?category=nice&catalog=url&pageid=youHaveThere
I want to call this function from inside an asp.net application. How do I do it?
I'm assuming you have the module already installed on your server, and are just asking about defining a specific rule. This is where you can read everything you need about creating rules.
The rules are all done through regular expressions, not my forte. But this is a format example:
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
The ([0-9]+) stands for number only patterns, ([_0-9a-z-]+) stands for text and number. I would think this would work for what you're tying to:
<rewrite>
<rules>
<rule name="Rewrite to Default.aspx">
<match url="^article/([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="app/Default.aspx?category={R:1}&catalog={R:2}&pageid={R:3}" />
</rule>
</rules>
</rewrite>
I wouldn't think you'd call a "rewrite rule". Instead you'd just use a Regex Replace. Maybe something like:
string nice_url = "http://webserver/nice/url/youHaveThere"
string new_url = Regex.Replace(nice_url, #"/(?<category>\w+)/(?<catalog>\w+)/(?<pageid>\w+)$", "app/Default.aspx?category=${category}&catalog=${catalog}&pageid=${pageid}");

Resources