Web Deploy deleting IIS website custom configuration - asp.net

I'm using Web Deploy (from VS2013) to publish an ASP.NET MVC site to an IIS 7.5.
I added some URL rewrite rules and custom HTTP response headers through IIS manager.
The problem is everytime I deploy a new version of the site, this extra configuration is deleted.
Is this the expected behaviour or is there something wrong? How can I keep these custom settings on each deploy?
UPDATE
So I understood I need to put these changes in the web.config. I'm trying to put them in the Web.Release.config but it's not being added to the deployed web.config. I guess I'm missing some XDT:Transform rule.
This is what I got in my Web.Release.config (yes, the publishing profile is using this Release config).
<configuration>
<!-- some other stuff -->
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Turn the build action of your web.config to None. That will prevent the file from being deployed each time you publish.
Edit
For inserting entire sections into a web.config from the web.release.config, you need the xdt:Transform="Insert" added like so:
<system.webServer xdt:Transform="Insert">
<rewrite>
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>

Ok so I understood I need to add this custom configuration in the web.config using XDT:Transform.
I added this to the Web.Release.config and it worked:
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>

Related

url rewrite / to default.aspx

We are experiencing some issues with our application where the default document (default.aspx) is not being picked up due the the appPool is set to Classic. It appears to be an issue documented here:
Site not redirecting to Default Document in Classic pipeline mode
to get around this we are trying to use the IIS URL Rewrite feature.
We want any request which comes to www.example.com/ to go to www.example.com/default.aspx
I used IIS to set it up on the root folder for the application, but i don't see it working, this is what it generated:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Default Documen Rewrite">
<match url="/" />
<conditions>
<add input="{QUERY_STRING}" pattern="/" />
</conditions>
<action type="Rewrite" url="/default.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
EDIT:
I just tried the following and it seems to be working, does anyone know if it's correct?
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="mysite/default.aspx" />
</rule>
</rules>
</rewrite>

Redirect single page to another domain

I need to redirect couple old webpages from my IIS to a new domain in a specific order.
For instance :
domain1/page1 to domain2/page1
domain1/page2 to domain2/page3
domain1/page3 to domain2/page5
...
Here's my webconfig <system.webserver> section:
<system.webServer>
<rewrite>
<rules>
<rule name="redirect single page" patternSyntax="ExactMatch" stopprocessing="true">
<match url="domain1/page1"/>
<action type="redirect" url="domain2/page1" appendquerystring="false"/>
</rule>
</rules>
</rewrite>
<httpredirect enabled ="true" destination"Domain2" httpresponsestatus="permanent"/>
</system.webServer>
But the redirecting is not happening it still points to the old domain pages.
Accordingly the #Claus answer on the question: 301 Redirect one domain to another using web.config
You can use the following code to achieve this:
<system.webServer>
<rewrite>
<rules>
<rule name="redirect" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www.domain1.com$" />
</conditions>
<action type="Redirect" url="http://www.domain2.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<system.webServer>
This example will match all URLs unless the hostname part is exactly www.domain1.com - and redirect those to www.domain2.com/whatever.

asp.net activate URL Rewrite IIS7

How to activate URL Rewrite in my project. I installed URL Rewrite, I see it in my IIS
When I add a simple rule in my web.config file
e.g. a rule just to remove default.aspx from the url or make url lower case
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>
<rule name="Default Document" stopProcessing="true">
<match url="(.*)Default.aspx" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
and when I run the project the rules don't work, please advice, thank you
I haven't tried above code, but did that long before.
I always followed this
http://www.iis.net/learn/extensions/url-rewrite-module/using-custom-rewrite-providers-with-url-rewrite-module
See if it helps you.

url rewrite -> for non www to www - which is better and how? (site level or application level)

I'm doing url rewriting (Wildcards) for a sigle web site based on
this blog post. This is what I tried:
<rewrite>
<rules>
<rule name="Redirect example.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
These codes works perfect and we can add them manually to web.config of web site or use url rewrite in IIS.
My problem is I have many web sites (domains and subdomains - net , com , org) installed on my IIS and I have to do a repeated job for all of them!
Is it possible to use another way for redirecting non www to www (site level or application level) for all web sites? If application level is possible which configuration files should I change? Would you please show us the correct wildcards or regular expressions?
You may edit your applicationHost.config (int the %systemroot%\System32\inetsrv\config directory) so that it includes common url rewriting rules for your IIS installation; the following two rules (one for HTTP, the other for HTTPS requests, if needed) do exactly what you are after:
<system.webServer>
<rewrite>
<rules>
<rule name="NonWwwToWwwRedirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}:{SERVER_PORT}" />
</rule>
<rule name="NonWwwToWwwRedirectSecure" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="on" />
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.+)$" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}:{SERVER_PORT_SECURE}" />
</rule>
</rules>
</rewrite>
</system.webServer>
That being said, I think it is not possible to restrict a given set of rules to a specific application pool, however.

Redirect to www in IIS7 - classic pipeline mode

I want to implement redirects on an IIS7 webserver. Basically, if the subdomain is not included in the URL, I will redirect to the www subdomain.
http://mysite.com/file.aspx redirects to http://www.mysite.com/file.aspx
http://mysite.com/image.jpg redirects to http://www.mysite.com/image.jpg
http://mysite.com/text.html redirects to http://www.mysite.com/text.html
How to do this?
I do not want to write any HTTP Module -- it must be done thru IIS config only.
Also, I am required to use Classic Pipeline mode and cannot install any ISAPI plugins.
Is it possible?
You can throw this into your web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^http://mysite.com$" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
In IIS7 it can be done through the url rewrite section.
This solution worked for me:
1) Install URL Rewrite component:
http://www.iis.net/download/urlrewrite
2) Add to web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite\.com$" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>

Resources