I need to use Url Rewriting, so I made a test case, in Web.config, to check if it's working:
Web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="Fail bad requests">
<match url=".*"/>
<action type="AbortRequest" />
</rule>
</rules>
</rewrite>
... other stuff
</system.webServer>
I was expecting any localhost:3285 to abort and fail, but it entered correctly.
I'm using Url Rewrite with IIS Express.
I am guessing your problem is browser caching. I have found that if I open a web page in IE, then add URL rewrite rules (requires stopping IIS express), then open the same page again in IE it still loads the page (and it appears the abort rule is not being applied). But, if I clear the IE browser cache and refresh the page the abort occurs.
Here is a full Web.config example that works for me
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="Fail bad requests">
<match url=".*"/>
<action type="AbortRequest" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Your example should work because the URL rewrite module is built into the current version of IIS Express:
http://www.iis.net/learn/extensions/introduction-to-iis-express/iis-express-overview
Related
I have Asp.net MVC 4 Application with WCF services. Project is hosted in iis Production Server P125 (10.10.10.125), there are few other sites hosted in same server. Our Web / Front End Server is working as Reverse proxy for production server (Url Rewrite and Application Request Routing installed) As,
185.132.x.x is public ip of Web Server acting as reverse proxy only (Windows Server 2016).
Our application is hosted at port 8899 of Application server ip 10.10.10.125
185.132.x.x:8080 <= reverse proxy => 10.10.10.125:8899
Website is working fine, all the pages/actions are working fine of MVC application, except the services (rest.svc , winact.svc)
whenever we hit 185.132.x.x:8080/rest.svc its showing error :
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /rest.svc
I dont know why ?
all our services are showing same error, while the services are working fine with 10.10.10.125:8899/rest.svc on local network as well as from the WebServer itself.
On remote login to WebServer(185.132.x.x) and browsing/accessing the application and services (.svc) via 10.10.10.125:8899, its working fine.
I am using the Reverse Proxy as :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://10.10.10.125:8899/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
After spending 48 hours i have figured out that, configuring serviceHostingEnvironment resolved the issue of services (.svc) and my Web.config on WebServer(185.132.x.x) is like :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<customErrors mode="Off"/>
<compilation debug="false" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5" />
<pages>
<namespaces>
<add namespace="System.Web.Helpers"/>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://10.10.10.125/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I have an HTML page in my old website which needs 301 redirect to the aspx page of new website, both websites have been built on asp.net platform. Please suggest me that how could I configure my web.config file to achieve this task.
At the moment I am using Meta Refresh to do this, but that is possibly 200 not 301.
Any help would be highly appreciated,Thanks.
I have used following piece of code in my old website web.config file, but it isn't working as well
<configuration>
<location path="http://example.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://newwebsite.com/test.aspx" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>
Create rules in your web.config file put
<system.webServer>
<rewrite>
<rules>
<rule name="URL1" stopProcessing="true">
<match url="^abc.html" ignoreCase="true" />
<action type="Redirect" url="Your current page path" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://uri" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>
Sorry I don't have a web.config solution for single page. You'll want to place this in your markup page near the top:
<% RedirectPermanent("http://url", true) %>
If it doesn't work for you post your markup here and I'll update it for you.
I'd like to set a request header (HTTP_HOST to be precise) from Web.config, using the IIS URL Rewrite module, on Azure Websites. Basically I'd like to have something like this in my site's Web.config:
<system.webServer>
<rules>
<clear />
<rule name="My rule" enabled="true">
<match url=".*" />
<serverVariables>
<set name="HTTP_HOST" value="my value" />
</serverVariables>
<action type="None" />
</rule>
This results in an error that HTTP_HOST is not allowed to be set. This is normal and with standard IIS the next step would be to add HTTP_HOST to the <allowedServerVariables> element to applicationhost.config directly or through AppCmd. However I couldn't find any hints on being able to access this config somehow.
Is it possible to somehow modify the apphost config, or add allowed server variables somehow else?
It is possible to alter Azure's ApplicationHost.config by applying xdt transformations.
Upload the file to the /site and restart your site for the changes to to take effect:
ApplicationHost.xdt
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<rewrite>
<allowedServerVariables>
<add name="HTTP_HOST" xdt:Transform="Insert" />
</allowedServerVariables>
</rewrite>
</system.webServer>
</configuration>
See also:
https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples
http://azure.microsoft.com/nl-nl/documentation/articles/web-sites-transform-extend/
Expanding on Joris' answer, you should use xdt:Transform="InsertIfMissing" and xdt:Locator="Match(name)" otherwise it won't work the way you expect it to (here's an example of it not working as-expected, and another example).
So your applicationHost.xdt should look like this:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<rewrite>
<allowedServerVariables>
<add name="HTTP_HOST" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)" />
</allowedServerVariables>
</rewrite>
</system.webServer>
</configuration>
I am new to Url rewrite rules so any help would be gratefully appreciated.
Looking to create a IIS Url rewrite that will handle any of the following
http://localhost/homepage/contact-us.aspx -> http://localhost/contact-us.aspx
http://localhost/homepage/about-us.aspx -> http://localhost/about-us.aspx
Any ideas?
I haven't tested this but I think you would have something like this in your web.config file.
You can edit the web.config file and then go into IIS and the rule should show up if you want to test it.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rewriterule1" enabled="true" stopProcessing="false">
<match url="homepage/(.*)" />
<action type="Rewrite" url="http://localhost/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Im trying to figure out how to setup a 301 permanent redirect for a website that is placed on a Microsoft-IIS/7.0 type server.
So let's say I have domain www.A.com and I want to redirect this to www.B.com I could use something like the following in my web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url="A.com" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.B.com$" />
</conditions>
<action type="Redirect" url="http://www.B.com/{R:0}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
When placing the web.config in the root directory, the server responds:
403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.
Any suggestion why this 403 error is given?
Thanks in advance.
If you are getting a 403 error with a plain (default) web.config and totally vanilla Default.aspx, then there is a configuration problem with IIS. Most likely the app pool does not have rights to the base folder for the website. Sounds like you're in a hosted situation so contact the system administrator.
This should work and also be much simpler. You don't need URL rewriting, just HTTP redirect.
As for the 403, does the IIS application pool have read access to the folder at the root of your site?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="www.B.com" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>