I would like to make a website always have www in the address, and enforce it via IIS rewrite.
For example, test.com would become www.test.com.
The typical example rewrite rule is:
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.test\.com$" />
</conditions>
<action type="Redirect" url="http://www.test.com/{R:1}" redirectType="Permanent" />
</rule>
However this requires me to enter the full url of my website. It will not work for development and staging environments that have URLs like www.test.dev and www.test.stage.
Is it possible to create an IIS Rewrite rule that will handle all of those cases?
I have another solution for you:
<rule name="Canonical domain name" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
You're right that the full URL needs to be in the web.config. You have options though.
You can use a config transform to make the regular expression match the correct environment.
There doesn't seem to be any harm if you include all three URL rewrite rules in your web.config. It sounds like your environments are isolated so each environment would only ever match one of the rules. That can clutter your web.config, but not horribly.
I'd go with option 1. You can find information on config transforms here: http://msdn.microsoft.com/en-us/library/dd465326.aspx
I would also suggest you another variant for testing in local environment:
Add to c:\Windows\System32\drivers\etc\hosts:
127.0.0.1 www.example.com
In IIS Manager select site and right click -> Edit bindings.. -> Add..
Enter host name: www.example.com
Open cmd and run iisreset
Now you are able to use www.example.com in browser which is mapped to localhost
Related
To try and make this as short as possible, I have two domains at work.
The first domain is our main domain at www.site1.com (port 80), pointing to a regular set up of a site in IIS 8, Server 2012.
The second domain is at www.site2.com (port 8085), but using some clever URL rewrite wizardry that I found on SO, www.site2.com points to www.site1.com/site2/, but behaves as if it were it's own domain. We did this in order to share assets between site1 and site2 in a .NET environment.
I do not have DNS-level control, but have admin of the server and it's settings and have done all of the URL rewriting for both sites myself. We received a request to install an SSL cert on www.site2.com (port 444) so that we can set the site to HTTPS. This is not relevant to the problem in itself because the problem exists in HTTP already.
The problem came about because another developer wants to be able to test the cert and the port configurations. As of right now, this isn't possible even in HTTP. Typing in www.site1.com:80 on the server works fine, but www.site2.com:8085 does not. site2 only connects properly when appended with www.site2.com:80 or just by typing in www.site2.com, I believe since it is rewritten as a subfolder off of site1.
I have tried everything I could think of in order to try and rewrite the ports in the URL so that the developer is able to test the port configuration by typing in www.site2.com:8085. The URL never resolves.
The bindings are set correctly as well because www.site2.com works fine in both test and production and has for years. It only does not work when the correct port is added to the URL.
This is the only code triggering the rewrite as it stands:
<rule name="www.site2 redirect" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com$" />
</conditions>
<action type="Rewrite" url="/site2/{R:0}" />
</rule>
Any and all help would be appreciated. Thanks.
You can do that in this way, just add one more condition into your rewrite rule:
<rule name="www.site2 redirect" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com$" />
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com:8085$" />
</conditions>
<action type="Rewrite" url="/site2/{R:0}" />
</rule>
Another way to to that with one condition is (remove $ from pattern in condiiton):
<rule name="www.site2 redirect" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com" />
</conditions>
<action type="Rewrite" url="/site2/{R:0}" />
</rule>
But this rule will capture domains like that as well: site2.com.au, site2.com.fr, site2.com:{ANYPORT} etc. That's why i prefer first rule
I have to redirect from a domain to another.
I try different ways to do this but it doesn't work.
Someone can tell me the right metod to do a redirect in web.config?
Without knowing the ways you tried, I would suggest setting up a Global URL Rewrite rule, you do this at the server level not the individual web site level.
These setting will be stored in the applicationhost.config (Server level), not the (web.config)
Below will redirect example2.com to example.com
<rule name="Redirect to new domain" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example2\.com$" />
</conditions>
<action type="Redirect" url="https://www.eample.com/{R:0}" />
</rule>
I am trying to redirect from my old ISP to my azure web site. I have set up the necessary DNS records. As I am using sub-folders on my Web App I need to set up redirection rules in the root web.config file to point from the domain name to the correct sub-folder of the main site.
It works perfectly when I have a redirect rule.
<rule name="Works" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Redirect" url="http://mysite.azurewebsites.net/mysub"/>
</rule>
However when I change to a Rewrite it fails. I really want a rewrite as I don't want the user to see the change of url in the browser.
<rule name="Fails" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Rewrite" url="http://mysite.azurewebsites.net/mysub"/>
</rule>
I get the following message:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
What am I doing wrong?
Your rewrite action should be a relative path, see the documentation:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rewrite_action
A substitution string must always specify the URL path
To configure an IIS rewrite rule to redirect traffic to my site to a different URL.
Basically I am trying to redirect this url: www.sharepointalex.co.uk to www.sharepointalex.co.uk/blog. At the moment it is pointing at the root of my site (a wedding website I setup for my partner and I).
www.sharepointalex.co.uk is a domain pointer on WinHost.
The rule I have so far is:
<rules>
<rule name="Blog" patternSyntax="ExactMatch" stopProcessing="true">
<match url="www.sharepointalex.co.uk" />
<action type="Redirect" url="{R:0}/blog" redirectType="Temporary" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.sharepointalex.co.uk" />
</conditions>
</rule>
</rules>
However this doesnt seem to work.
Any help would be greatly appreciated.
Cheers
The match URL should not include the host name.
I've been using the UrlRewrite IIS plugin for about a month on our production site.
I created a single redirect rule using the supplied template/wizard, the resulting config enrty is as follows:
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mycompany\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mycompany.com/{R:1}" />
</rule>
</rules>
</rewrite>
It's been running fine until this morning, when the site started erroring with "too many redirects". As far as I know, nothing in the configuration or infrastructure changed.
I disabled the rule, and the site became functional again (though clearly without any redirecting).
I then re-enabled the rule, and now all is running as expected. I didn't make any changes to the rule other than to temporarily disable it.
Any ideas? Is the plugin buggy?
I'd recommend setting this up:
http://learn.iis.net/page.aspx/467/using-failed-request-tracing-to-trace-rewrite-rules/
This may help you track down the problem if you start getting the "too many directs" error again.
Try this other code, i have on my web and run perfect:
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yourdomain\.com$" />
</conditions>
<action type="Redirect" url="http://www.yourdomain.com/{R:0}" redirectType="Permanent" />
</rule>
The explanation is simple:
Match any URL received to process
The condition is that have anydomaintext.extension (your domain and extension) without prefix
Redirect to same domain with full prefix and put all url.
Other tries was R:1 but quit some of the main url and not run.
The sample from Ruslani:
http://blogs.iis.net/ruslany/archive/2009/04/08/10-url-rewriting-tips-and-tricks.aspx
I tried to use adding www but finally use the sample above.
The fix below worked for me. I discovered my rewrite rule was at out-of-date. The other domain had changed their URL policy and were now redirecting all traffic from otherdomain.com to www.otherdomain.com
<action type="Rewrite" url="http://otherdomain.com/abc/{R:1}" />
to
<action type="Rewrite" url="http://www.otherdomain.com/abc/{R:1}" />
Do you see the difference? By adding the 'www' I preempted the other domain redirection. I basically just complied with their new policy.