web.config - Ignore rewrite for specific folder - c#

The root is a WordPress site. I need to ignore URL rewrite for a folder "app", as I hosted a virtual directory .NET App on that folder.
http://www.mywebsite.com/app
URL Rewrite rules in web.config works partially. If the URL link to a static file or folder in the .NET app, it works. If the URL link to a routed url, it fails. How to fix?
For example this URL:
mywebsite.com/app/Login
shoud be routed to
mywebsite.com/app/Login.aspx
but, it is captured by WordPress and display a 404 not found error.
if the URL is entered as this, then it runs ok:
mywebsite.com/app/Login.aspx
Here is the web.config at root site (wordpress)
<rule name="WordPress: http://www.mywebsite.com" patternSyntax="Wildcard">
<match url="*"/>
<conditions>
<add input="{R:0}" pattern="^/app" negate="true" />
<add input="{R:0}" pattern="^/app/(.*)$" negate="true" />
<add input="{R:0}" pattern="^app/(.*)$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
</conditions>
<action type="Rewrite" url="index.php"/>
</rule>
This is the code for URL routing in the .NET App
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("login", "login", "~/login.aspx");
}
}

I managed to solve the problem by using this line:
<add input="{HTTP_URL}" pattern="*/app" negate="true"/>
There is a nice reference by Microsoft that explains in detail how these rules work.

Related

How to fix page refresh issue with Angular and MVC?

I am using angular 5 inside MVC 5 using and I am using angular routing inside this. Everything working fine but when there is an angular route URL on the browser address bar and I am doing F5 it's giving the error- The resource cannot be found.
I am sure its because this URL pattern does not match with my MVC routing but it matches with angular routes.
How to solve this?
It is simple to solve this problem. Do this in your imports modules:
RouterModule.forRoot(routes, { useHash: true }).
In this case, the Url after the hash is not sended anymore to your server side.
For more information go here.
Odds are you need to have the UrlRewriter module installed for IIS and then you need to have the following in your web.config at system.webServer/rewrite/rules
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/Home/Index?url={UrlEncode:{R:0}}" />
</rule>
The specifics of the rewrite rule though will depend on your app. I'm hooking up my angular root html node in my Home/Index view. If you are doing that differently, you will have to update it to route to where you are expecting it to go.
Angular docs

How can I re-route a specific subdomain request to a different ASP.NET MVC controller?

I have an ASP.NET MVC 4 web app. It has a default home page that handles all requests to http://www.mydomain.com as well as http://mydomain.com. I also have an MVC Area set up that can be accessed via www.mydomain.com/Foo/Hello/ (where "Foo" is the name of the area and "Hello" is a controller in that area).
If someone makes a request to foo.mydomain.com right now, it will route them to the default home page controller. I would like all requests to the foo subdomain root (eg. without a specific area/controller specified) to automatically be rerouted to the /foo/hello/ controller.
Additionally, I want any requests to the "Foo" Area via the www subdomain to be redirected to the "foo" subdomain. ie. I want all requests to www.mydomain.com/Foo/Goodbye to be automatically redirected to foo.mydomain.com/Foo/Goodbye
I have, of course, looked at lots and lots of other samples/questions, but none of them seem to address my exact issue.
I'm not sure if I should solve this issue with routes, route constraints, route handlers, etc.
Additionally: this application is hosted on Windows Azure Cloud Services, so I don't have full control over IIS settings.
In your Web Server, the Application Pool of your site must have Integrated PipeLine Mode as highlighted below..
or you can find it through the Basic settings of your Application Pool like below..
Then I added the HttpModule in my sample MVC application
HttpModule
public class MyModule1 : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += context_BeginRequest;
}
public void Dispose() { }
void context_BeginRequest(object sender, EventArgs e)
{
if (!System.Web.HttpContext
.Current
.Request
.Url
.Authority
.Contains("foo.mydomain.com"))
{
string URL =
(System.Web.HttpContext.Current.Request.Url.Scheme + "://" +
"foo.mydomain.com" +
HttpContext.Current.Request.Url.AbsolutePath +
HttpContext.Current.Request.Url.Query);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.StatusCode = 301;
System.Web.HttpContext.Current.Response.Status
= "301 Moved Permanently";
System.Web.HttpContext.Current.Response.RedirectLocation = URL;
System.Web.HttpContext.Current.Response.End();
}
}
}
Then i added some code n Web.config for my HttpModule
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<add name="MyModule1" type="rewrite.MyModule1" />
</modules>
<handlers>
<remove name="UrlRoutingHandler" />
</handlers>
</system.webServer>
<system.web>
<httpModules>
<add name="MyModule1" type="rewrite.MyModule1" />
</httpModules>
</system.web>
Then in the host file I added the following code.
127.0.0.1 foo.mydomain.com
Then I added the Bindings for my website
You can use URL Rewrite module for IIS7+ to achieve what you described. Documentation is here: URL Rewrite Module Configuration Reference.
Simplified example would be:
Rewrite URL so that foo.mydomain.com access /foo/hello (so no
redirect)
Redirect URL www.mydomain.com/foo to foo.mydomain.com/foo
Add in your web.config, system.webServer node rewrite section:
...
<rewrite>
<rules>
<rule name="Redirect mydomain.com/foo to foo.mydomain.com/foo" patternSyntax="Wildcard" stopProcessing="true">
<match url="foo" negate="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://foo.mydomain.com/{R:0}" redirectType="Temporary" />
</rule>
<rule name="Rewrite foo.mydomain.com to access /foo/hello" patternSyntax="Wildcard" stopProcessing="true">
<match url="" />
<conditions>
<add input="{HTTP_HOST}" pattern="foo.mydomain.com" />
</conditions>
<action type="Rewrite" url="/foo/hello" />
</rule>
</rules>
</rewrite>
</system.webServer>
You can also edit/view those rules in IIS Manager, using GUI. Click your web site and go to IIS section / URL Rewrite module. Bellow you can see 1st rule in GUI.

Redirect website from http to https

I have a website stored on azure for which I got an SSL certificate. I am trying to redirect www.mywebsite.com to https://www.mywebsite.com but with no success.
I am using the following code that should change the configuration of IIS where my project is deployed :
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{URL}" pattern="/$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
But when I type my URL it does not redirect to https.
By the way, rewrite appears unrecorgnized by Intellisense.
I can see that you've taken the code snipet from Steve Marx example
The problem is that you have whitespace in your rule name. Just remove them and it will work.
The name attribute of rule must not have spaces; the rule won’t work correctly in IIS on Azure with spaces in the name.
Find the full article here:
Azure https guide

Redirect subfolder using HttpHandler

I have my site asp net pages at www.mydomain.com, and the blog was inside www.mydomain.com/blog. I had to move the blog to a subdomain, so I need to write a module to redirect any calls for any page within the blog to the new subdomain.
It works in my machine but not when I upload to the shared hosting. Any ideas what is wrong?
I wrote the following code
public void ProcessRequest(HttpContext context)
{
string sourceUrl = #"www.mydomain.com/blog";// #"localhost:51393/blog"
string destinationUrl = #"blog.mydomain.com/blog";
string currentLocation = context.Request.Url.AbsoluteUri;
if(currentLocation.ToLower().Contains(sourceUrl))
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.StatusCode = 301;
System.Web.HttpContext.Current.Response.AddHeader("Location", currentLocation.Replace(sourceUrl, destinationUrl));
System.Web.HttpContext.Current.Response.End();
}
}
And added these handlers
<httpHandlers>
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
<add verb="*" path="*" type="MyHandler,App_Code.dll"/>
Any help is deeply appreciated.
I know it is pretty similar to this one httpHandler - subfolder issue but it didn't work.
Just use IIS Rewrite
Here's a similar code that redirects 1 URL to another. Little tweak will do it. I got this working with my smarterasp.net hosting account.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Removing the .aspx extension from the web application

I am new to URLRewriting and trying to remove the .aspx extension using the following script in my web.config
<configuration>
<configSections>
<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
</configSections>
<connectionStrings>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to clean URL" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
<action type="Redirect" url="{R:1}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
However, I have no success with this. Moreover, the following code block is giving me an error.
<httpHandlers>
<add verb="*" path="*.aspx"
type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
> Could not load file or assembly 'URLRewriter' or one of its
> dependencies. The system cannot find the file specified.
Do I need to add the rewrite engine to my web application?
I have gone through this link but I could not get it.
Can any one suggest to me a step by step process or sample script please?
Use the following rule, works like a charm everytime!
<rule name="san aspx">
<!--Removes the .aspx extension for all pages.-->
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
Here is some nice rewritting engine with how-to's :)
http://www.codeproject.com/Articles/2538/URL-Rewriting-with-ASP-NET
http://www.codeguru.com/csharp/.net/net_framework/article.php/c19535/RewriteNET--A-URL-Rewriting-Engine-for-NET.htm
Seems like you have not added the dll Reference in the Web project for the class URLRewriter. Add this reference to fix this issue.
(As alternatives to writing your own)
IIS 7 URL rewrite module :
http://www.microsoft.com/en-gb/download/details.aspx?id=7435
IIS 5/6 :
http://iirf.codeplex.com/

Categories