Redirecting non www, index.html, 404 - asp.net

I have created code to redirect for non www to www, /index.html to "/", 404 to custom page, I am using IIS server and putting this code in web.config file, have a look, its working fine. But I want to know is it good for SEO ? Or It need any modification ?
Thanx
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="index\.html(?:l)?" />
<conditions>
<add input="{HTTP_HOST}" pattern="example\.com.au$" />
</conditions>
<action type="Redirect" url="http://www.example.com.au/" />
</rule>
<rule name="CanonicalHostNameRule2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com.au$" />
</conditions>
<action type="Redirect" url="http://www.example.com.au/{R:1}" />
</rule>
</rules>
</rewrite>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/404error.html" />
</httpErrors>
</system.webServer>
</configuration>

Your rules look fine. In general, the idea with making your URLs cannonical is to direct all the link juice to a single cannonical address. In the eyes of Google and such, www.mydomain.com and mydomain.com are two different URLs. Same goes for mydomain.com/ and mydomain.com/imndex.html. The only thing I would add to your rules is a statusCode of 302. This will tell both Google and client`s browsers that you would like them to reffer to the target URL from now on. This way Google will sum all link juice to the target and also clients browsers will not need to be re-redirected with each request.

Related

web.config :: redirect all except root/index.html (Azure, WordPress)

I have a Wordpress hosted on azure, that I have move from www.mydomain.com to old.mydomain.com,
on www.mydomain.com I make a new landing page that inform of what I want to inform and give a link to the "old" wordpress,
to avoid loosing seo (and keep wp info alive) I made the next web.config
<?xml version="1.0"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://old.mydomain.com" httpResponseStatus="Permanent" />
</system.webServer>
<location path="www.mydomain.com">
<system.webServer>
<httpRedirect enabled="false" />
</system.webServer>
</location>
</configuration>
redirect works perfectly, but it also redirect me www.mydomain.com to old.mydomain.com
How can I do it in web.config this no-redirect-the-root ??
I have try also the :
<location path="index.html">
<system.webServer>
<httpRedirect enabled="false" />
</system.webServer>
</location>
but the result is the same, everything gets redirect to old.mydomain.com
You can try to use the Rewrite module of IIS, please consider the following configuration:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Root rule" stopProcessing="true">
<match url="^$" />
<action type="None" />
</rule>
<rule name="redirect rule" stopProcessing="true">
<match url="^(.*)$" />
<action type="Redirect" url="http://www.example.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

IIS redirect from subdomain to path

Suppose i have real subdomain like this.
test.example.com, is it possible to write a rule for iis to redirect subdomain to test.example.com/SomeStaticPage.html
the rule should work for multi level subdomains, ie.
test.aaa.bbb.example.com should be redirected to
test.aaa.bbb.example.com/SomeStaticPage.html
Using URL Rewrite is very easy, in fact I'm wondering if I don't get the requirements exactly. (do you need different static pages per domain?, or is it really just to redirect to the same "SomestaticPage.html" while keeping the host name when no default page is specified?)
A simple rule like this would do what your question asks:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/SomeStaticPage.html" />
</rule>
</rules>
</rewrite>
<system.webServer>
</configuration>
Now, if you were looking for a more complete solution that allows you to specify a page based on the host name, then you could use a rewrite map where you add the host names that should be redirected and which page they should go to. For example the rule below will check if they are requesting "/" and the host name is in the rewrite map and will use that page to redirect, if it is not in the list then it will not do anything:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="{C:0}" />
<conditions>
<add input="{HostMap:{HTTP_HOST}}" pattern=".+" />
</conditions>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="HostMap">
<add key="test.example.com" value="/SomeStaticForTestExample.html" />
<add key="test.a.b.c.example.com" value="/SomePageForTestABC.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>

404 redirect for pages only not images

I am working with an asp.net mvc application. I have the following entry in web.config to handle 404's
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/Error/Error404" responseMode="ExecuteURL" />
</httpErrors>
This works fine for when pages are requested, it redirects to my 404 view. However for missing images it also redirects to the 404 page ie. the response for the image is the 404 page.
As this is a performance issue, is there any way I can alter the above so that only 404 from "pages" and not resources such as images trigger a redirect to the 404 page?
I know this is an year old question, however I just came across the same problem, and I was able to come up with a solution based on what I was using in htaccess.
The below solution basically checks if the file has an image extension, and it isn't a file and isn't a directory. After that it serves up an image file that I use to identify missing images.
<rule name="404 Images" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions>
<add input="{URL}" pattern="^.+\.(jpg|jpeg|png|gif|ico)$" ignoreCase="true" negate="false" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
</conditions>
<action type="Rewrite" url="/design/images/404.png" />
</rule>
You could disable runAllManagedModulesForAllRequests:
<system.webServer>
<modules runAllManagedModulesForAllRequests="false" />
...
</system.webServer>
Of course now you're gonna see IIS default 404 page for broken images since static resources will be directly served by the static handler and not going through the managed pipeline.

Replacing IIS rewrite rules in web.config transform

I have some IIS rewrite rules that I want to vary by environment. The development rewrite rules are in the web.config file, then at the end of the web.test.config file I have:
<appSettings>
...Some app settings tranforms here
</appSettings>
<system.webserver>
<rewrite xdt:Transform="Replace">
<rules>
... rules here
</rules>
</rewrite>
</system.webserver>
</configuration>
My app settings are getting transformed when I deploy to test, but by IIS rewrite rules are not. I was hoping the entire <rewrite> section would simply be replaced with the one in the transform file (as per http://msdn.microsoft.com/en-us/library/dd465326.aspx), but nothing is changing.
I have tried putting xdt:Transform="Replace" xdt:Locator="Match(name)"> on the individual rules too:
<rule name="Test rule" stopProcessing="true" xdt:Transform="Replace" xdt:Locator="Match(name)">
But again this makes no difference.
Is it even possible to replace rewrite rules in the web.config and if so, what am I missing?
As I didn't have any rewrite rules in my main web.config, the Replace transform didn't work. I successfully used the Insert transform, as below:
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mysite\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
There is a lot of answers here with examples which is a good thing, but I think few details are missing. I have wrote about this in my website, the key point here is to add xdt:Transform="Insert" in the root tag hierarchy you want to be added for the respective environment.
By default you have your Web.config file, but you have also Web.Debug.config and Web.Release.config as seen in the image below:
Lets say you want to added a redirection from http to https in your release of the application. Then edit Web.Release.config and add following lines:
<?xml version="1.0"?>
.....
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
......
</rules>
</rewrite>
</system.webServer>
</configuration>
So next time you publish your project the tag with rewrite and its sub-content will be added to web.config file.
To see that before you publish, right click on Web.Release.config and click Preview Transform.
You will see the difference between initial version and release version.
Reference:
HTTP to HTTPS Redirect - IIS 8.5 not working properly
Microsoft Web.Config file transformations
Disclaimer: the link of this guideline refer to my personal web site.
The rewrite section worked weirdly to me at first when creating the release configs, errors and sections not showing at all. This is how i solved it.
Microsoft (R) Build Engine version 12.0.31101.0
Microsoft .NET Framework, version 4.0.30319.0
Edit After messing about with this i realized that having the rewrite tag on a server that does not have the rewrite plugin make the webserver return an error. I want different configurations on server and local development machine so the fix is:
The un-transformed web.config only needs a <system.webServer> tag and in the web.config.release for a basic canonical host name rule
<configuration>
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="CanonicalHostNameRule" xdt:Transform="Insert">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.host\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.host.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The action didn't need a name at all but the rewrite tag needs the xdt:Transform="Insert"
Obviously if you want it on your local machine as well, it would need an update instead.
It is possible to transform the rewrite section of system.webServer. I was initially having the same problem and realized that I had inadvertently placed the rewrite node incorrectly under system.web. While this does not look like your problem based on the limited snippet that you provided, I would still suspect that your issue is related to node placement in the transform file.
Here is what my Web.Debug.config looks like (and this version is writing the correct Web.config on a debug build):
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an atrribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
<system.webServer>
<rewrite xdt:Transform="Replace">
<rules>
<clear/>
<rule name="Canonical Hostname">
<!-- Note that I have stripped out the actual content of my rules for the purposes of posting here... -->
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
A trick I use is give the action a name
then in my transform just add xdt:Transform="SetAttributes" xdt:Locator="Match(name)" like the following
<system.webServer>
<rewrite>
<rules>
<rule name="RedirecttoWWW" enabled="true" >
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action name="AddWWW" type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</rule>
</rules>
</rewrite>
The above example is to add www to all requests
-------UPDATE-----
just an update adding name to the action will not work as wanted so I updated the code as the following
<system.webServer>
<rule name="RedirecttoWWW" enabled="true" xdt:Transform="RemoveAll" xdt:Locator="Match(name)" >
</rule>
<rule name="RedirecttoWWW" enabled="true" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)" >
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

Windows Server Web.config Strip Index Filename

I am using the following web.config file to redirect the non-www version of a site to the www version. However, I would also like to have it strip the file name of the index file as well.
For example:
redirecting www.example.com/index.html to www.example.com
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="CanonicalHostNameRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Edit:
Here is my updated config file. But, its causing a 500 error, now.
See CodingGorilla's answer below :)
In order to get rid of the index.html after the redirect, drop the {R:1}. But then you will need to modify that rule so that it triggers only for /index.html requests and create a new rule that triggers on other pages that includes the {R:1} so that requests for example.com/mypage.html will still get redirected properly.
Edit:
Edit #2
And the final answer is!
Based on our chat conversation, I think this is the final rule set:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="index\.htm(?:l)?" />
<conditions>
<add input="{HTTP_HOST}" pattern="example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/" />
</rule>
<rule name="CanonicalHostNameRule2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Resources