Rewrite ASP.Net MVC homepage to another IP address - asp.net

I have been trying to rewrite my homepage to another ip address in my ASP.Net MVC application. I have installed URL Rewrite and Application Request Routing modules in IIS. I have set the rewrite in my web.config, when i change the action type to redirect it works perfectly, but once i change it back to rewrite it does not work at all.
I have changed the matching url to accept all requests, but still it does not work.
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="node" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<action type="Rewrite" url="http://255.255.255.255/" />
</rule>
</rules>
</rewrite>
</system.webServer>
It seems like rewriting process has some conflicts with routing. How could i make the priority of rewrite higher?

Firstly you have to configure an Application Request Routing Server Farm that will consist of only one server (you should specify an IP for this server). After that you need to configure a global URL rewrite rule as described here.

The configuration of the URL Rewrite Module in the <rewrite> section is for internal URLs, and cannot rewrite domain or IP of the server:
IIS.NET: A Rewrite action replaces the current URL string with a substitution
string. A substitution string must always specify the URL path (for
example, contoso/test/default.aspx).
What you asked is not an url rewrite, but a reverse proxy. If you have an access to IIS, you can try its reverse proxy configuration, or simply create an HttpHandler, similar to this example. Next option can be http://urlrewriter.codeplex.com

Would there be config inheritance happening?
If so, you can wrap the section you want with <location> to disable inheritence from parent app...
<location path="." inheritInChildApplications="false">
<system.webServer>
...
</system.webServer>
</location>

Related

IIS 7.5 404 not found for all images via one of the http bindings

I have ASP.NET MVC site on IIS 7.5/Windows Server 2008 R2. Site have a set of http bindings to different domains, such as domain1.test.dev.com. domain2.test.dev.com, 10 bindings in total. They all are doing well, except one. Every request for an image(jpg, gif, png) returns 404 for this single binding. So entire layout has been loaded, all others requests feel good, but all images are broken with error 404 Not Found. There are no https bindings, they all are using http, port 80 and IP Address is 'All Unassigned'.
Could anyone help please?
The answer is found. There was an additional web.config file in ~/Images directory. This config contained specific redirection rule for one of the domain, so for this domain I saw 404, and others worked great. An additional web.config appeared in ~/Images directory accidentally, because of missclicking
in URL Rewrite module on the IIS. Some time ago we have used such redirections for demo and removed them after that. The redirection rule in web.config was a kind of:
<system.webServer>
<rewrite>
<rules>
<rule name="ProductionRedirectDomain1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{CACHE_URL}" pattern="*domain1.test.dev.com*" />
</conditions>
<action type="Redirect" url="{C:1}domain1.com{C:2}" />
</rule>
</rewrite>
</system.webServer>
The bug has disappeared after removing a redundant config. Thanks, everybody!

IIS URL Rewrite does not work for externally hosted Wordpress with pretty permalinks

We are hosting our site on IIS 7.0 and Wordpress-based blog with a third-party provider. We have implemented Reverse Proxy, so folder link from www.mysite.com/blog goes to Wordpress subdomain (which originally has blog.mysite.com URL). The only configuration was made on IIS side
Everything works fine if we use standard permalinks in Wordpress - www.mysite.com/blog/?p=7 but I receive 500 error if we switch to "post-name" permalinks - www.mysite.com/blog//%postname%/.
Most of the questions and solutions I found are about URL rewriting if Wordpress is hosted on the same IIS as the main site but I couldn't find anything related to our situation.
Do I need alter the Wordpress in some way (e.g. rules on .htaccess) to cooperate with my IIS rewriting? I am not that familiar with Apache and PHP...
Updated: web.config content
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://blog.mysite.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
You need to setup reverse proxy in IIS that requires a combination of URL Rewrite along with ARR. Refer this article for more details:
Reverse Proxy with URL Rewrite v2 and Application Request Routing

A very simple IIS7 redirect to different path

I have had a site at www.mysite.com/myfolder/ for years. I have developed a new version and want to just remove the /myfolder so that the site serves all pages from www.mysite.com.
Example: www.mysite.com/myfolder/mypage.htm will be permanently redirected to www.mysite.com/mypage.htm
How do I configure this using the URL Rewrite module in IIS7? I start with a blank rule, but then get lost. Thanks.
You either use the IIS Management UI or edit the web.config directly. With a section in web.config that looks like this, you will redirect everything (images as well), which might be want you want.
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="^myfolder/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
If you use the Management tool, you add an empty rule, give it a name, add ^myfolder/(.*) as the pattern, select Redirect as action type, add {R:1} as target URL and select 301 as redirect type.
This is supposed to be permanent, that's why you should use a 301 redirect (for SEO purposes and so on).

Why does a reverse proxy rewrite rule stop working when System.Web.Routing is active?

Boiling this down to the simplest possible rule:
<rule name="Reverse Proxy" stopProcessing="true">
<match url="^external/(.*)" />
<action type="Rewrite" url="http://some-site/{R:1}" />
</rule>
("Enable Proxy" is checked in the ARR Server Proxy settings at the server level).
The above rewrite rule works fine in a very simple test app with a web.config containing the section, it works fine in a web forms app, but if I put the same rule into an MVC3 app (on the same machine, so identical config for IIS higher up) it never has any effect; the request flows through.
if it's just a rewrite (and not a reverse proxy) it works OK, e.g.,
<rule name="rewrite to internal" stopProcessing="true">
<match url="^internal/(.*)" />
<action type="Rewrite" url="different-internal/{R:1}" />
</rule>
...is fine.
I can get the reverse proxy rule to work if I add
routes.IgnoreRoute("external/{*pathInfo}");
in the Global.asax.cs class, so that my request for external/* doesn't hit the default controller, but I don't understand why. I think the URL rewrite module kicks in way before the Routing (see http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/) so I'd expect there not to be a conflict between them.
Is the Routing module adding in "virtual" rewrite rules to the URL rewrite module, that are overriding my declared rewrite rules?
I had this exact same issue and it took me the entire day to find the solution.
Find: the ServoceModel tag in your Web.config file and add the serviceHostingEnvironment code as seen below:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
This will allow routes to be passed through to IIS to handle it.
One more tip, I recommend anyone having routing issues with their MVC projects to install Remote Debugger via NuGet. This will tell you what routes are being activated when.

redirect all requests to www.example.com to example.com in config without access to IIS

I'm currently planning to deploy a site with a third party hosting provider. I will only have access to the server via ftp and a tool similar to cpanel called WebsitePanel.
No access to IIS set up or configs.
Is there anyway to redirect http://www.example.com to http://example.com?
Place this in your web.config using your values for domain.com. This leverages the URL rewrite rules of the web.config and IIS 7.
<system.webServer> / <rewrite> / <rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:1}"
redirectType="Permanent" />
</rule>
Typically, the "tool similar to cpanel" should give you this option.
Failing that, you should be able to:
a) set a custom 404 page pointing, to, say, myredirector.asp [or whatever server-side script you wish to use]
b) in myredirector.asp [or whatever] , do a server-side redirect as appropriate.
Not as clean as a straight IIS redirect, but it works pretty good.
I'd suggest you do this through the domain's DNS configuration, rather than through your application. It's much simpler and doesn't rely on application code to work (so if you deploy a whole new application, you don't have to remember to add any config entries or similar).
Same thing can be done to add the prefix www also. A blog post for the same at following URL:
http://karmic-development.blogspot.in/2013/10/add-prefix-www-automatically-in-url-in.html

Resources