I have many global IIS7 URL Rewrite rules and they by default apply to all sites. Well there are several sites that i would like to disable this rewrite inheritance on for all rules. How can I do this? I tried the following without joy:
<rewrite>
<rules>
<clear />
</rules>
</rewrite>
Sorry, that cannot be done:
Global rewrite rules are used to define server-wide URL rewriting
logic. These rules are defined within applicationHost.config file
and they cannot be overridden or disabled on any lower configuration
levels, such as site or virtual directory. Global rules always
operate on the absolute URL path (that is, requested URI without the
server name).
and
Global rule set is always evaluated first, and after that distributed
rule set will be evaluated by using a URL string produced by global
rule set.
http://learn.iis.net/page.aspx/468/using-global-and-distributed-rewrite-rules/
If you are able to move the functionality you need out of the global rules and into the web.config for your sites, you can add a condition on each rule to either opt-in or opt-out of each rule based on the presence of a local file for that site. This would allow you to deploy a common set of code and config, but with local/per-site customization.
Example of an opt-in rule:
<rule name="HTTP to HTTPS Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" />
<!-- Opt-in to rule via presense of a local file -->
<add input="{DOCUMENT_ROOT}/Local/rewrite-rule-enable-https.txt" matchType="IsFile" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
Note: The Local folder is not deployed, but rather used to store per-site files.
So essentially this rule does not turn on unless a file called "rewrite-rule-enable-https.txt" is found in the /Local folder on site.
Related
I am wondering if it is possible to set up a rewrite rule where you do not specify the domain, only the page accessed and the page to redirect to. My goal for this is to not have to have different versions of the same rule for different sites (i.e. dev.mysite.com, qa.mysite.com, etc), so one rule works for multiple subdomains. For example, I need to redirect someone going to dev.mysite.com/mexico to mx.dev.mysite.com/es/mexico, but that also has to work for QA. Additionally, production has a slightly different domain which would make this even more difficult. This is what I came up with - it definitely needs work:
<rule name="Test Mexico Redirect">
<match url="^mexico"/>
<action type="Redirect" url="http://mx.{R:2}/es/mexico" redirectType="Permanent"/>
</rule>
Unfortunately it just does not work; I get a blank page. Probably because {R:2} is looking for the domain in the rule and one isn't specified. I can't think of a way to do this, because not even the domain is the same across all environments.
It would be cool if you could use regex in the <action> element, then I would do something like this: ^(mx\.).*com/es/mexico.
It's possible using {HTTP_HOST} and back references like {C:#}. The IIS Rewrite module is extremely useful for setting this up. Here is a screenshot with some notes that explains how it's done:
By using {HTTP_HOST} and conditions I was able to make a rewrite rule that can apply to all environments. The actual XML result:
<rule name="Mexico Redirect" stopProcessing="true">
<match url="mexico" />
<conditions>
<add input="{HTTP_HOST}" pattern="^((?!mx))" />
<add input="{CACHE_URL}" pattern="^(.+)://" />
</conditions>
<action type="Redirect" url="{C:1}://mx.{HTTP_HOST}/es/search" redirectType="Temporary" />
</rule>
Also I may have been incorrect saying you won't need to recycle the app pool with a Temporary (307) redirect. I still needed to recycle it but only in production... no verdict on this yet.
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 building an ASP.NET CMS-driven web application which will serve multiple websites under different domain names. Some of these will use www sub-domain, others will use custom sub-domains. There will be a variety of top-level country domains.
I'm looking for a generic IIS URL Rewrite rule that will redirect any URL which doesn't specify a sub-domain to its www equivalent.
When I say generic it means the rule cannot hard-code either domain name or top-level country domain. So the rule must redirect
http://anything.anywhere/any-path to http://www.anything.anywhere/any-path but leave http://sub.anything.anywhere/any-path.
The closest I've found is this which still hard-codes TLCD. Without much knowledge of the syntax of URL Rewrite I'm not sure how to handle any TLCD.
Thanks in advance.
Update:
Inspired by comment, I've had a go with regex, but haven't yet found a method that doesn't require me to hard-code a list of all possible TLCDs. I suspect this is the best I'll get. Can anyone refine or confirm this as the answer?
^([a-z]+[.](com|co.uk|de|fr|etc)+)*
I just did the exact same thing using a rewrite rule with two conditions, one to get the Scheme and one to determine if the www is missing. The scheme is necessary as the redirect has to be absolute, but if your not catering for HTTPS that could be hard-coded. Just bear in mind I've not had time to test the HTTPS part yet, but pretty sure it will work ok.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Root Redirect" stopProcessing="true">
<match url=".*" negate="false" />
<action type="Redirect" url="{C:1}://www.{HTTP_HOST}/{R:0}" />
<conditions trackAllCaptures="true">
<add input="{CACHE_URL}" pattern="^(.*)://" />
<add input="{HTTP_HOST}" pattern="^(?!www\.).*" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
That title isn't very descriptive, but I couldn't figure out how to phrase my question very well. What I'm trying to do is use a single page to interpret multiple URLs. Here's an example: [domain]/name-of-question.aspx is clearly not a file on the site's server, and yet the server acts like it is. This behavior makes pages much more readable and more easily bookmark-able.
My vision for the solution is to be able to have to server redirect a request to a certain directory to a particular page, whilst appending the name of the page requested to the page as a URL parameter. Here's what I mean: [domain]/questions/name-of-question redirects to [domain]/question.aspx?page=name-of-question.
This is how reddit does their self posts, I think, but they don't use ASP.Net or IIS.
Is this possible, and if so, how would one implement this behavior? If there's any code you write, please write it in C#, because I don't know VB.Net very well. Thanks!
You need to use URL rewriting to accomplish this.
You have to create a rewrite rule that rewrites any requests to [domain]/questions/{1}
to [domain]/question.aspx?{1}
In ASP.NET you have the URL Rewriter module: http://www.iis.net/downloads/microsoft/url-rewrite
The rule might look similar to this and is applied in the web.config file:
<rewrite>
<rules>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="questions/(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="question.aspx?{R:1}" />
<serverVariables>
<set name="{RESPONSE_CONTENT_TYPE}" value="image/png" />
</serverVariables>
</rule>
</rules>
</rewrite>
EDIT: To change the content type, add the serverVariables section in the rewrite rule and authorise that variable to be set in IIS manager:
If you are using ASP.NET 4.0+, then this might be worth a read, as ASP.NET Routing and URL Rewriting are not necessarily competing technologies, but potentially complementary features.
URL Rewriting vs. ASP.NET Routing
I would like create URL rewrite rule that will set default document for my virtual folders. eg. someting like this
www.domain.com/en/ -> www.domain.com/en/index.aspx
www.domain.com/hr/ -> www.domain.com/hr/index.aspx
www.domain.com/de/ -> www.domain.com/de/index.aspx
directories en, hr, de doesn't really exists on web server they are just markers for languange used in site used by home grown http module that will rewrite path with query params.
Quick solution was define rule for every single lang, something like this :
<rewrite>
<rewriteMaps>
<rewriteMap name="Langs">
<add key="/en" value="/en/index.aspx" />
<add key="/hr" value="/hr/index.aspx" />
<add key="/de" value="/de/index.aspx" />
</rewriteMap>
</rewriteMaps>
<rules>
But I would really like solution that would not require changes in web.config and adding rewrite rule for every languange used on particular site.
Thanks !
<rule name="Lang-Redirect">
<match url="^(\w{2})\/?$" />
<action type="Rewrite" url="{R:1}/index.aspx" />
</rule>
That should allow you to capture the language tag from the request and rewrite it to your custom http handler.