URLRewrite IIS 7 Help Needed - asp.net

Here is my settings for IIS 7 URL Rewrite:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="^Articles/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="articledetails.aspx?articleid={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Unfortunately, my page at the following url is not displayed at all:
http://www.highoncoding.com/Articles/723_Introduction_to_IPhone_Development.aspx

Looks like you are giving in the wrong URL.
<match url="^Articles/([0-9]+)/([_0-9a-z-]+)" />
The above rule will match /Articles/723/Introduction_to_Iphone_Development. You are giving it /Articles/723_Introduction_to_Iphone_Development (underscore instead of slash). Seems that you fixed it as I am typing this though :)

Related

url rewrite / to default.aspx

We are experiencing some issues with our application where the default document (default.aspx) is not being picked up due the the appPool is set to Classic. It appears to be an issue documented here:
Site not redirecting to Default Document in Classic pipeline mode
to get around this we are trying to use the IIS URL Rewrite feature.
We want any request which comes to www.example.com/ to go to www.example.com/default.aspx
I used IIS to set it up on the root folder for the application, but i don't see it working, this is what it generated:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Default Documen Rewrite">
<match url="/" />
<conditions>
<add input="{QUERY_STRING}" pattern="/" />
</conditions>
<action type="Rewrite" url="/default.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
EDIT:
I just tried the following and it seems to be working, does anyone know if it's correct?
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="mysite/default.aspx" />
</rule>
</rules>
</rewrite>

How to route ALL requests for domain to a sub folder asp.net

I have seen lots of solutions to this but none seem to work....
I have a domain
producerpte.co.uk
But I have a separate application set up in a sub folder so to see the root of that application you have to go to...
producerpte.co.uk/producerpte
However I want it so that the user does not see the 'site1' part of the url.
When I use url rewriting in Web.Server config or I use Context.RewritePath in code (in an application set up on the root) it simply redirects the user to the url (with site1 in it) I do not want the user to know they are going to a subfolder.
Am I setting this up wrong?
I'm actually using Winhost. All the examples ive tried just do not change result of the call without redirecting the url :-(
I have been told by winhost that I should do it with code/configuration. I would prefer to do it with code to be honest....
UPDATE : I tried Max's answer and I did...
<rules>
<rule name="Redirect if producerpte" stopProcessing="true">
<match url="^ producerpte/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder">
<match url="^.*$" />
<action type="Rewrite" url="producerpte/{R:0}" />
</rule>
</rules>
But no luck :-( And the urls still change in the browser....
If WinHost did enable IIS Rewrite Module, edit your web.config file and give this rule a try:
<rewrite>
<rules>
<rule name="Redirect if site1" stopProcessing="true">
<match url="^site1/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder">
<match url="^.*$" />
<action type="Rewrite" url="site1/{R:0}" />
</rule>
</rules>
</rewrite>
--- UPDATE ---
Well, I tested this IIS configuration:
I've added rules on the root site.
IIS created a web.config file:
Here's the content of the web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect if producerpte" stopProcessing="true">
<match url="^producerpte/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder">
<match url="^.*$" />
<action type="Rewrite" url="producerpte/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
http://localhost:8066/ displays the content of C:\inetpub\wwwroot\producerpte\producerpte without changing the URL in browser
http://localhost:8066/producerpte/ displays the content of C:\inetpub\wwwroot\producerpte\producerpte but the URL in browser is changed to http://localhost:8066/
Be sure that you created rules on the root site (and not the sub application) and I noticed that you had a leading space in your config: <match url="^ producerpte/(.*)$" /> between ^ and producerpte.

IIS 7 url rewrite module on web.config

can anybody help me with this please. I have IIS7 and the hosting says that the URL Rewrite module has been installed already. I even put into the bin directory the Microsoft.Web.Iis.Rewrite.dll
i can only use the web.config for the url rewrite configuration since I dont have access to the IIS manager (hosting restriction).
here is my code on the web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to list_cities.aspx">
<match url="^/state/([a-zA-Z]+)" />
<action type="Rewrite" url="~/list_cities.aspx?state={R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
I still get the error, HTTP Error 404.0 - Not Found, when i go to
http://xxxxxxxx.com/state/CA/ . I have already searched the issue but I cant seem to find any solutions.
Can anybody please check my code. Thanks a lot.
I found the solution just now (not being an IIS expert...)
Change {R:1} for {R:0}
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to list_cities.aspx">
<match url="^/state/([a-zA-Z]+)" />
<action type="Rewrite" url="~/list_cities.aspx?state={R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>

IIS URL Rewrite and Web.config

I don't understand anything about IIS, but am trying to solve this problem of redirecting all visitors to example.com/page to example.com/page.html
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRedirects">
<add key="/page" value="/page.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration>
A couple of problems arise:
I don't know where to even put the file. There is a User root directory, and an htdocs directory, I tried both, no joy.
I don't even know if the account can do rewrites, I am trying to find that out.
1) Your existing web.config: you have declared rewrite map .. but have not created any rules that will use it. RewriteMap on its' own does absolutely nothing.
2) Below is how you can do it (it does not utilise rewrite maps -- rules only, which is fine for small amount of rewrites/redirects):
This rule will do SINGLE EXACT rewrite (internal redirect) /page to /page.html. URL in browser will remain unchanged.
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRewrite" stopProcessing="true">
<match url="^page$" />
<action type="Rewrite" url="/page.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
This rule #2 will do the same as above, but will do 301 redirect (Permanent Redirect) where URL will change in browser.
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^page$" />
<action type="Redirect" url="/page.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
Rule #3 will attempt to execute such rewrite for ANY URL if there are such file with .html extension (i.e. for /page it will check if /page.html exists, and if it does then rewrite occurs):
<system.webServer>
<rewrite>
<rules>
<rule name="DynamicRewrite" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}\.html" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/{R:1}.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
Just wanted to point out one thing missing in LazyOne's answer (I would have just commented under the answer but don't have enough rep)
In rule #2 for permanent redirect there is thing missing:
redirectType="Permanent"
So rule #2 should look like this:
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^page$" />
<action type="Redirect" url="/page.html" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Edit
For more information on how to use the URL Rewrite Module see this excellent documentation: URL Rewrite Module Configuration Reference
In response to #kneidels question from the comments; To match the url: topic.php?id=39 something like the following could be used:
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^topic.php$" />
<conditions logicalGrouping="MatchAll">
<add input="{QUERY_STRING}" pattern="(?:id)=(\d{2})" />
</conditions>
<action type="Redirect" url="/newpage/{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
This will match topic.php?id=ab where a is any number between 0-9 and b is also any number between 0-9.
It will then redirect to /newpage/xy where xy comes from the original url.
I have not tested this but it should work.
Just tried this rule, and it worked with GoDaddy hosting since they've already have the Microsoft URL Rewriting module installed for every IIS 7 account.
<rewrite>
<rules>
<rule name="enquiry" stopProcessing="true">
<match url="^enquiry$" />
<action type="Rewrite" url="/Enquiry.aspx" />
</rule>
</rules>
</rewrite>

How can I correctly rewrite a URL with .aspx to a URL without .aspx?

I have a site that currently uses the .aspx extension on its pages. It's getting a Joomla conversion and the .aspx extension will not work anymore. I need it so that if someone enters the .aspx extension, it will just get removed the URL so none of the SEO on the current site breaks.
For example, I need
www.arrc.com.php5-17.websitetestlink.com/services/managed-services.aspx
to be rewritten/redirected to
www.arrc.com.php5-17.websitetestlink.com/services/managed-services
This is what I have in my web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Migrate to PHP">
<match url="^([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
<rule name="Rewrite .aspx">
<match url="^([_0-9a-z-]+)/?([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The first URL match is for any URLs that have a URL like services.aspx instead of services/managed-services.aspx
Whenever I go to www.arrc.com.php5-17.websitetestlink.com/services/managed-services.aspx it gives me an internal server error, but www.arrc.com.php5-17.websitetestlink.com/services.aspx rewrites correctly. What can I do to fix this?
They are greedy, switch the order.
<rule name="Rewrite .aspx">
<match url="^([_0-9a-z-]+)/?([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}/{R:2}" />
</rule>
<rule name="Migrate to PHP">
<match url="^([_0-9a-z-]+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
Two potential issues:
I think for rewriting, it's going to hit them in order, which means the first rule is going to always kick off. Try switching the order of the rule.
The dash should work correctly in [_0-9a-z-], but try escaping the dash.

Resources