Is there a way to configure a web.config file to execute it's rules only if there is a match to a server name?
For example, we have two servers: WebDev (for development) and WebLive (live web server)
On the live server, we have a URL rewrite rule that configures a canonical name, but when we synchronize those sites (thereby copying the web.config file back to the dev) and run it on the development platform, the canonical rule kicks in and send us to the live site instead of keeping us on the development platform. I can exclude the web.config from our synchronization, but was seeking an alternative.
Kinda like...
<apply rule to WebLive>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.org$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.domain.org/{R:1}" />
</rule>
</>
In Visual Studio 2010, you now can apply a transformation to your web.config depending on the build configuration.
When creating a web.config, you can expand the file in the solution explorer, and you will see two files:
Web.Debug.Config
Web.Release.Config
They contains transformation code that can be used to:
Change the connection string
Remove debugging trace and settings
Register error pages
link
Related
This issue is not related to application configurations (custom), but more to do with IIS settings.
So I need the following to be in the web.config when i create a publish for my app.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
</configuration>
However, when debugging i only want the part and not the http redirect (If i try to debug my app with the rewrite in the web.config it does not start)
in previous asp.net, we could have multiple web.configs for debug and release and it would transform when published.
I simply want to the all of the above code to be in the web.config when published, and only part to be in applied in web.config when i am debugging
This isn't a true answer to your question, but I've got what I think is a much better solution overall. For some time, I've found the fact that URL Rewrites have to go into the Web.config to be frustrating. As careful as you are, it's almost inevitable that you're going to overwrite the Web.config at some point, removing rewrites that have been added to it. This is especially the case if a developer doesn't know better and adds a rewrite directly through IIS, but never copies it over to the project's Web.config in source control (which happens more often than not).
As a result, I started creating a site in IIS just for redirects like this. It has nothing but a Web.config, and then I add the bindings that I'm redirecting from to it. For example, for a rewrite like this, you'd add the binding for the HTTP version of your domain to the redirect site and the HTTPS binding to the actual web application site. Then, you can create the rewrite rule on the the redirect "site", and never ever worry about accidentally overwriting it, because you never publish anything there. This would effectively side-step your issue here, entirely.
I have several shared hosting accounts with Newtek. They told me one account can be used to host several websites, but I don't know how to do that.
They said it can be done using either an .htaccess or web.config file. And since it's ASP.NET, that suggests web.config is the way to go.
Does anyone know how to use web.config to route requests for a given domain to a subfolder? I assume that subfolder would need to be set as an application starting point, which it appears their control center will allow me to do. I just need to associate that starting point with a particular domain.
And does this sound like a reasonable approach (shy of forking out money for a virtual server)?
If the IIS URL Rewrite Module is installed, you can create a Rule to redirect the request to a subfolder based on the domain name.
<rule name="site2.com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com" />
<add input="{PATH_INFO}" pattern="^/site2/" negate="true" />
</conditions>
<action type="Rewrite" url="\site2\{R:0}" />
</rule>
Source: https://weblogs.asp.net/owscott/iis-url-rewrite-hosting-multiple-domains-under-one-site
I am trying to forward a specific URL on my website to another website and I can't figure out how to set the proper values in the web.config file for my IIS server.
For example I would like to forward
www.example.com/ballot
to
ballot.example.com
So far I've tried the following but it doesn't have any affect to the url
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^/ballot$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://ballot.example.com/" />
</rule>
</rules>
</rewrite>
I'm hosting the website on a Rackspace Cloud site.
Okay, I figured this one out and I'm posting the solution here for anyone else that may come across a similar situation.
If you want to redirect a certain URL like http://www.example.com/ballot to http://ballot.example.com/
Then follow these steps
If one doesn't already exist create a directory called ballot directly under the web root of www.example.com.
Create a Web.config file and place it in the ballot directory with the following content.
Make sure you change the destination to your actual destination.
If you are hosting on a Rackspace cloudsite you must rebuild the application (not sure about other host). You can rebuild the application by deleting the Web.Config file (if one was present) and uploading a new one, or by clicking the rebuild application link within the General Settings tab of the cloudsite you are trying to redirect. More information about rebuild applications in Rackspace here: http://www.rackspace.com/knowledge_center/article/how-to-rebuild-an-aspnet-application-in-cloud-sites.
Also here is a link to the page where I ultimately found the solution to what I was trying to accomplish: https://www.stokia.com/support/misc/web-config-response-redirect.aspx
All, I was stuck with a problem when I deploy a web site. I found there is an element named rewrite in the web.config.
<rewrite>
<rules>
<!-- below rule will abort api request when the request to pattern "apis/v.*" is of "http" method-->
<rule name="AbortApiHTTPRequest">
<!-- Note:
the below pattern is assumed that all apis conain prefix "apis/v", e.g. apis/v3/aaauth
if there are some exceptions for the assumption, below pattern needs to be updated.
-->
<match url="^apis/v.*$" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="AbortRequest" />
</rule>
<!-- below rule will redirect all non-https requests except for above requests to https request.-->
<rule name="RedirectToHTTPS" stopProcessing="true">
<match url="^.*$" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:0}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
When I remove the element everthing is ok, the website works well. Otherwise I got a error says.
HTTP Error 500.19 - Internal Server Error The requested page cannot be
accessed because the related configuration data for the page is
invalid.
After I did some research, I found this question talking about it. I am not sure if it is the cause of the problem. seems it is just a xml syntax validation of Visual studio. Does it really matter with the deployment of web site in IIS 7?
Anyway, I also followed the instructions of the post , But I failed to get it works .even I run the cmd as the administrator. the error says below.
Failed to open file Xml\Schemas\DotNetConfig.xsd. Make sure that the
script is run in the elevated command prompt.
I wandered if I have the enough previlige to run cscript command ? thanks.
More
If I run the project in the Visual Studio with the Asp.net development server, It can work without any error. But If I published the project to the IIS in my computer. It doen't work.
The rewrite directive is used by URLRewrite module for IIS.
If the module is not installed, you will get an error similar to the above. You can either install the module if you need URLRewriting - or - comment out the entire <rewrite> section.
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