Url Rewrite Regex to Match Some Classic ASP Files (But not all) - asp.net

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$

Related

IIS URL rewrite dynamic number of folders

I have to create a rewrite rule that catches all URLs from the old shop system and redirect them to the new shop. The problem is the following:
The source URL from the old shop can contain the path where you did find the product number, looks like this: www.domain.com/folder1/folder2/folder3/[product number].html
folder1, folder2 and folder3 can, but don't need to be in the called URL. They also are not fixed (e.g. folder1 can be "cars" or "bikes" or "services"). The link could also be just www.domain.com/folder1/[product number].html or even www.domain.com/[product number].html sometimes.
For the new system, I only need the [product number] in my target URL. Should look like this: www.domain.com/path1/path2/[product number].aspx
I could not find anything in Google or Stackoverflow that helped me with this.
Thanks in advance
Daniel
This should work:
<system.webServer>
<rewrite>
<rules>
<rule name="Three deep" stopProcessing="true">
<match url=".*/.*/.*/(.*?).html" />
<action type="Redirect" url="/path1/path2/{R:1}.aspx" />
</rule>
<rule name="Two deep" stopProcessing="true">
<match url=".*/.*/(.*?).html" />
<action type="Redirect" url="/path1/path2/{R:1}.aspx" />
</rule>
<rule name="One deep" stopProcessing="true">
<match url=".*/(.*?).html" />
<action type="Rewrite" url="/path1/path2/{R:1}.aspx" />
</rule>
<rule name="None deep" stopProcessing="true">
<match url="(.*?).html" />
<action type="Rewrite" url="/path1/path2/{R:1}.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
I am worried that the expression - (.*?) - I'm using for the product code is too catch all, but I don't know the structure of your product codes. If you could tighten it up, it'd be better.
I'm also worried that they will hit things that aren't products (e.g. category and search) so if there is a pivot point that all product URLs use, I'd recomend using that. Tightening the product code bit will probably help false matches too.
At the moment it's working on the .html vs .aspx being the bit that isn't sending it into a redirect spiral.
I can probably do it in less rules with a better regex, which I'll have a bash at tonight.

Ensure www using webconfig rewrite

I'm working with an Umbraco asp.net website and I would like to ensure website url is alwayes displayed with www I have added:
<rule name="WWW rule" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
To me this is a good methode for making sure only one URL on website. Only issue as I see is that when typing DOMAINE only my default.aspx will be added to frontpage.
How to avoid that?
I have tried to add:
<rule name="Default Document" stopProcessing="true">
<match url="(.*)default.aspx" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
This makes frontpage inside a loop....
Have I misunderstood somthing? Thanks
Shouldn't stopProcessing="true" for the domain rewrite? I'm guessing you don't want the rest of the rules evaluated?
Also, I'm guessing you might want to use {HTTP_HOST} in you condition, since I would guess that the {CACHE_URL} would be the one including the default.aspx path?
Currently you are using the incoming http/https schema, which are also considered different url's, so you may want to choose which one to use, or implement a canonical meta tag on your site to tell search engines which schema to use.

IIS URL rewrite module url's to lowercase

For better SEO we are using URL rewrite to convert all the URL's to lowercase. I set this one as mentioned in this the below article.
Everything is working fine from URL perspective, but we see lot of 301 redirects when we check in fiddler.
It looks like the images, javascript, css, jquery ajax calls and everything is getting converted into lower case.
I am trying to remove that and want to rewrite only aspx extension and no extension urls. I tried to play around the matchurl without any success. Any help or guidelines will be highly appricated.
Thanks
Edit:
My Current rule is
<rules>
<rulename="LowerCaseRule1"patternSyntax="ExactMatch"stopProcessing="true">
<matchurl="[A-Z]"ignoreCase="false"/>
<actiontype="Redirect"url="{ToLower:{URL}}"/>
</rule>
</rules>
You could probably use something as follow:
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="\.aspx$" />
<add input="{REQUEST_FILENAME}" pattern="\." negate="true" />
</conditions>
</rule>
The rule will be triggered only if one of the condition is true:
The first one checks if the requested path (filename) ends with .aspx.
The second one checks if the if the requested path (filename) doesn't contain a . (so doesn't have an extension)

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>

IIS url rewrite | How to remove directory and extension?

I have been struggling with the following for quite some time now:
Default url:
examplesite.com/folder/about.cshtml
Desired url:
examplesite.com/about
Basically I want to accomplish two things:
1 Remove the file extension with realtively compact code.
2 Remove the folder that houses the about page.
I have found some uncommon rules to achieve all the above, but they mostly contain a lot of redundant code that crashes my site when I test it with IIS 8.0.
So I was hoping someone could share a rule that is compact and fits my needs. Or seperate rules with the same outcome.
Every contribution is much appreciated :)
I'm not certain I entirely understand your needs, but here's something that's at least close. It strips out the first folder and file extension (so examplesite.com/folder/about.cshtml becomes examplesite.com/about and examplesite.com/folder/help/about.cshtml becomes examplesite.com/help/about). If you wanted to strip all folders then just remove the ?.
<rule name="Remove Directory and Extension">
<match url="^(.*?)/(.*)\.cshtml$" />
<action type="Rewrite" url="{R:2}" />
</rule>
Update:
Ok, I think what you want is a combination of two rules then:
<rules>
<rule name="Redirect requests to friendly URLs">
<match url="^(.*?)/(.*)\.cshtml$" />
<action type="Redirect" url="{R:2}" />
</rule>
<rule name="Rewrite friendly URLs to phsyical paths">
<match url="^(.*)$" />
<action type="Rewrite" url="folder/{R:0}.cshtml" />
</rule>
</rules>
The first rule makes sure that all requests are to friendly URLs. The second takes the friendly URL and rewrites it to your physical path, where the physical path is folder/[FRIENDLY_PATH].cshtml.

Resources