I have MyWebSite and MyApp as shown below:
MyWebSite
web.config
MyApp
web.config
Pages
page.aspx
I want the next URL will work:
www.mywebsite.com/oldpage.aspx
MyWebSite\web.config add to the URL 'MyApp': www.mywebsite.com/MyApp/page.aspx
MyApp\web.config add to the URL 'Pages': www.mywebsite.com/MyApp/Pages/page.aspx
I want to separate this logic in two rules because I want each rule will responsible for its part. MyWebSite should decide which application is default, if not specified, and it should not know anything about 'Pages' in MyApp.
The problem, that the rule in MyApp is not called.
The question: do I do something wrong? Is this is limitation that I can't perform rewrites in separate config files?
P.S. MyWebSite and MyApp running under the same app pool.
MyWebSite web.config:
<rule name="Add 'MyWebSite' for ASPX pages">
<match url="^([^/]+\.aspx)" />
<action type="Rewrite" url="MyApp/{R:1}" />
</rule>
MyApp web.config
<rule name="Add 'Pages'">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="Pages" negate="true" />
</conditions>
<action type="Rewrite" url="Pages/{R:0}" appendQueryString="true" />
</rule>
What you want to do seems possible reading the following blog post: http://www.iis.net/learn/extensions/url-rewrite-module/using-global-and-distributed-rewrite-rules
However, the only way I succeeded in getting it to work was using a dirty trick. I am not sure it would be recommended in a production environment but in case you want to go with this solution:
MyWebSite web.config:
<rule name="Add 'MyWebSite' for ASPX pages">
<match url="^([^/]+\.aspx)" />
<action type="Rewrite" url="http://www.mywebsite.com/MyApp/{R:1}" />
</rule>
I just changed the Rewrite destination to url="http://www.mywebsite.com/MyApp/{R:1}" (I have ARR up and running as well).
Related
We would like to redirect users from below URL
http://mywebsite.com/Account.asp?Number=25191108
To
http://anotherwebsite.com/Account/25191108
Please let me know if we can do it by config at IIS level or in web.config
You can use URL Rewrite module in IIS to achieve this. You can install it from here.
Add the following rule in your web.config file under "system.webServer" tag.
Let me know if it breaks or if you have any questions around this.
<rewrite>
<rules>
<clear />
<rule name="sample2" stopProcessing="true">
<match url="(.*).asp(.*)Number=(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="http://anotherWebsite.com/{R:1}/{R:3}" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
ASP.NET 4.5 Webforms website
I have two domains:
company.com
company.co.ca
Both pointing to the same site and the default document is set to login.aspx.
When someone goes to company.co.ca, I check the Request.ServerVariables["HTTP_HOST"] in the login.aspx.cs and redirect users to company.co.ca/ca/login.aspx
I thought I can use the url rewrite and have this rule in my web.config but it doesn't redirect but stays on the main login.aspx.
<rewrite>
<rules>
<rule name="CA HomePage" stopProcessing="true">
<match url="company.co.ca/login.aspx" />
<action type="Redirect" url="company.co.ca/ca/login.aspx" />
</rule>
</rules>
</rewrite>
What am I doing wrong? Is there a better way?
We are planning to add one more domain and so I don't want to do a manual check in login.aspx.cs and redirect and want to find a more general best way.
You check for the host like this with IIS rewrites:
<rule name="CA HomePage" stopProcessing="true">
<match url="^login.aspx$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^company.com$" />
</conditions>
<action type="Redirect" url="http://company.co.ca/ca/login.aspx" />
</rule>
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.
I have the following rule on the site to redirect http to https. We just found out though that our app got submitted with just an http for the api. Until we can get this updated I need the site to ignore calls to the /api folder and only redirect everything else. I'm sure there's a way to say something like if URL does not contain /api/ then redirect.
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
Add an entry similar to <add input="{R:0}" pattern="/api(/|$)(.*)" negate="true" /> so that the whole file is:
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{R:0}" pattern="/api(/|$)(.*)" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
Example URL: http://site.com/api/function
So, if the URL after the site matches any of the following it will stop processing (and thus not push the user to https)
/api
/api/anything
Any https URL
We run into the same kind of thing with a large application run in IIS behind a reverse proxy. The URL rewrite addon for IIS (that you appear to be using) is a bit of a pain, but it does the job really well and tolerates the MVC framework.
As you mentioned, simply putting a rewrite block in an API directory won't work because with MVC there are no directories. You would think MS would have a better solution for this -- but they don't. It makes things all the more challenging.
If you place a separate Web.config file in the /api application or directory you can override whatever rules apply for the site as a whole.
Check out Tip #1 in this article, and if you have the time read them all:
http://weblogs.asp.net/jgalloway/archive/2012/01/17/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides.aspx
John Galloway's blog is a fantastic resource for all things IIS and ASP.NET.
I'm trying to use the rewrite rule from the HTML 5 Boilerplate project to make circumventing browser cache (aka cache busting):
<rewrite>
<rules>
<rule name="Cachebusting">
<match url="^(.+)\.\d+(\.(js|css|png|jpg|gif)$)" />
<action type="Rewrite" url="{R:1}{R:2}" />
</rule>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://chewsy.com{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
If i try to access my css with /css/all.123456.css, it fails to find the file with the error reporting that it's looking for /css/all.123456.css (no rewrite). I've tried commenting out the "Remove WWW" rule to see if that was a conflict, but same behavior.
Any ideas why this rule isn't being applied and rewriting the URLs?
Update: I'm using these settings for my web server in VS2010:
<match url="^(.+)\.\d+\.(js|css|png|jpg|gif)$" />
<action type="Rewrite" url="{R:1}.{R:2}" />
I pressume you want to get /css/all.css, if not, post the desired result...
EDIT: VS internal development server (Cassini) doesn't support IIS URL Rewriting Module, you would have to use IIS (Express) for that, or some third party component (http://urlrewriter.net/)...