HTTP Error 404.4 - Not Found - URL Rewrite - IIS Express - asp.net-mvc

I am building an application using ASP.NET MVC + AngularJS. Now to make API calls through AngularJS, I am using URL rewrite because APIs are hosted on different servers.
but it gives me 404.4 error. I checked the trace for the request and patterns are matching fine. It is trying rewrite to the server url, but then it gives 404.4 error.
I have also added in my web.config. My rewrite rule is as follows:
<rewrite>
<allowedServerVariables>
<add name="HTTP_Authorization"/>
</allowedServerVariables>
<rules>
<rule name="Api Call" stopProcessing="true">
<match url="quote/(.*)" />
<action type="Rewrite" url="http://<server>/pc/service/edge/quote/{R:1}" appendQueryString="false" />
<serverVariables>
<set name="HTTP_Authorization" value="Basic <key>" />
</serverVariables>
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="quote/(.*)" />
</conditions>
</rule>
</rules>
</rewrite>
What am I doing wrong?
Please help. Thank you in advance.

You need to install IIS Application Request Routing: Visit https://www.iis.net/downloads/microsoft/application-request-routing
Check the “Enable proxy” checkbox located in Application Request Routing feature view is IIS Manager

Related

Replace %5E in URL using IIS Rewrite

I have the following problem:
I send a request via HttpResponse.Redirect method to a web application:
http(s)://localhost/application?arg1=abc^def
This works pretty fine on IE/Edge/Chrome/Firefox. But on Safari I get the problem that the request is sent as following encoded string:
http(s)://localhost/application.aspx?arg1=abc%5Edef
The application does not like/accept that and I am not able to perform changes on the application.
Is it possible to rewrite the URL on the webserver where the application is hosted by IIS Rewrite module somehow?
I tried already to create a rule with following regex pattern:
^application?(.\*)%5E(.\*)
and the following action:
Rewrite URL:
application.aspx?{R:1}%5E{R:2}
...but this does not seem to work (although the regex pattern has passed the "Test Pattern" test)
Many thanks in advance!
This is because there is a problem with your rewrite rules. The following configuration will rewrite http://localhost/application.aspx?arg1=abc%5Edef to http://localhost/application?arg1=abc^def:
<rewrite>
<rules>
<rule name="TEST" stopProcessing="true">
<match url="(application.aspx)" />
<action type="Rewrite" url="http://localhost/application?{C:1}^{C:3}" appendQueryString="false" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)(%5E)(.*)" />
</conditions>
</rule>
</rules>
</rewrite>

Web requesting forwarding to another domain

I have a site "MySite" setup in my IIS with the below HTTP bindings,
www.example.com:80
www.example.net:80
www.example.org:80
I need to temporarily redirect all users coming to www.example.net to www.example.com/product.
Any ideas how to achieve this ?
Hi Siva Sankar Gurram,
You could use below url rewrite rule to redirect url www.example.net to www.example.com/product by using below url rewrite rule in iis:
<rule name=".net to .com" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.net$" />
</conditions>
<action type="Redirect" url="http://www.example.com/product" redirectType="Temporary" />
</rule>
Note: If you did not installed Url rewrite module first install it.

Custom Error Change defaultRedirect

I run a Sitecore multi site environment (c#/MVC). The same IIS website, same folder, same code is used for multiple websites. For example www.chalk.com and www.cheese.com, very different looking sites but both use the same IIS website (host headers), same folder, same web config, same database backend.
All is good except the error page. In the web.Config I have the usual setting:
<customErrors defaultRedirect="/error.html" mode="RemoteOnly" />
I need something like
<customErrors mode="RemoteOnly">
<error host="chalk.com" defaultRedirect="/chalkerror.html">
<error host="cheese.com" defaultRedirect="/cheeseerror.html">
</customErrors>
Is something like this possible?
In case anyone finds this question...
for me, I found the answer in Rewrite Rules.
NOTE: You need the URL Rewrite IIS Extention installed. (Download Here)
I added to the Web.Config
<system.webServer>
<rewrite>
<rules configSource="folder\filename.config" />
</rewrite>
</system.webServer>
And then a config file (filename.config)
<?xml version='1.0' encoding='utf-8'?>
<rules>
<rule name="Error-Chalk" patternSyntax="Wildcard" stopProcessing="true">
<match url="error.html" />
<action type="Rewrite" url="chalkerror.html" />
<conditions>
<add input="{HTTP_HOST}" pattern="*chalk*" />
</conditions>
</rule>
<rule name="Error-Cheese" patternSyntax="Wildcard" stopProcessing="true">
<match url="error.html" />
<action type="Rewrite" url="cheeseerror.html" />
<conditions>
<add input="{HTTP_HOST}" pattern="*cheese*" />
</conditions>
</rule>
<rules>
This line <add input="{HTTP_HOST}" pattern="*cheese*" /> makes the match on the domain name, so www.cheese.com or image.cheese.com will match. <match url="error.html" /> matches the page being asked for. <action type="Rewrite" url="cheeseerror.html" /> is the page IIS will serve. I can now have the different error page for the different site.
Apparently, this isn't achievable. customErrors settings work to redirect to error pages targeted for specific HTTP error codes.
However, As you mentioned, its an MVC application, you can create a Custom Error Handler MVC filter which checks for host\domain and based on that redirects to the required ErrorView.
Also, here are 6 great methods described to achieve expected behavior MVC Exception Handling

Remove Subdomain from URL in IIS or ASP.net MVC or haproxy

Is it possible to remove a subdomain (or basically treat it like www) from the URL?
Example:
subdomain.domain.com/specific/link/forsubdomain -> domain.com/specific/link/forsubdomain
and not have it point to the primary domain and return a 404 error,
example:
domain.com/specific/link/forsubdomain -> return a 404 because it only exists in the subdomain.
If it's possible to do something in Haproxy as well Or disguising URLs in ASP.net MVC Route table modifications im open to it.
Not just IIS configuration.
Just wanted to know if it's possible to change the URL as i described and still have it point to the subdomain site.
If I understand your question correctly you are refering to canonical URLs.
Within IIS you can use URL Rewrite, which is a module you can install manually or via the Web Platform Installer (if it's not already installed).
This allows you to create a url rewrite rule which you can configure per site, something along the lines of;
<rule name=”Redirect URL to WWW version”
stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^example.com$” />
</conditions>
<action type=”Redirect” url=”http://www.example.com/{R:0}”
redirectType=”Permanent” />
</rule>
You can add the rules manually to the site's web.config file or use a GUI within the IIS site manager tool.
There's a lot of reference and a number of tutorials available from Microsoft and numerous bloggers.
An example of a complete web.config just doing this type of redirect.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="WWW Redirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^contoso.com$" />
</conditions>
<action type="Redirect" url="http://www.contoso.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Redirect to non-www-url domain with MVC4 / IIS7.5

First of all, I just spent a few hours on Stackoverflow reading similar questions but no answer seems to work for me.
I have an MVC4 application which has an SEO issue : the non-www is not redirected to www. I have installed the Rewrite module on the server (IIS7.5) and on my local environment (VS2010).
I tried many ways but none seems to work...
Ideally, I would prefer to have the rewrite rule in my MVC4 solution and avoid doing server side configuration as it is more convenient when deploying to new servers.
I tried to put the following code block in my web.config :
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<!-- code added for SEO non-www to www redirect -->
<rewrite>
<rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<!-- SEO code -->
</system.webServer>
I then published the solution (with the right domain name) but nothing changes. When trying to access http://domain.com, I just get a 404 not found error.
I tried several code block for this part but it just never work. I also tried to configure this on the server, which didn't work as well. The best solution would be to have this rewrite in my web.config so the rules are applied whenever I publish to a new web server.
Does anyone sees something wrong with this ? My web.config is in my MVC4 application and published via the Publish menu we get when right-clicking the solution in VS2010.
Thank you very much in advance for your replies.
I also tried this block for the rule but it doesn't change anything..
<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>

Resources