Redirecting default.aspx to root virtual directory
I have a simple ASP.NET 3.5 application running under IIS7 under a virtual directory. So the URL of my app is like http://example.com/app. I want to 301-redirect the request to example.com/app/default.aspx to example.com/app for better SEO. I have to do this redirect through code only, not by any IIS settings. I am unable to do so via code mentioned in this 开发者_运维问答article:
https://web.archive.org/web/20211020203216/https://www.4guysfromrolla.com/articles/072810-1.aspx
The code:
if (request.RawUrl.Equals("/default.aspx"))
{
newUrl = string.Format("{0}://{1}{2}",
request.Url.Scheme,
request.Url.Authority,
request.RawUrl.Remove(request.RawUrl.LastIndexOf("/default.aspx", StringComparison.OrdinalIgnoreCase)));
context.Response.Status = "301 moved permanently";
context.Response.AddHeader("Location", newUrl);
}
seems to go into an infinite loop when the application is under a virtual directory. the request.RawUrl property always returns "/default.aspx" even after a 301 redirect causing the infinite loop. How can I fix this issue?
thanks,
Asif
The above code will work fine as long as you dont have a sub-directory. AFAIK, its a bug in ASP.NET: the Request.RawUrl should NOT contain "/default.aspx" when the URL does not have that extension. I have tested your code and it works fine without a sub directory, but if default.aspx is under a directory, the Request.RawUrl object fails to get rid of default.aspx and hence the infinite loop.
Slight modifications to handle subdirectories and maintain url variables. Better comparison and replace functionality probably exists, but this works for me with IIS 7.
if (Request.RawUrl.ToLower().Contains("/default.aspx")) // use Contains instead of EndsWith to handle url vars
{
string newUrl = string.Format("{0}://{1}{2}",
Request.Url.Scheme,
Request.Url.Authority,
Request.RawUrl.ToLower().Replace("default.aspx", "")); // don't remove the trailing slash so url vars are maintained
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", newUrl);
Response.End();
}
I tried the URL Rewrite method. I.e. this one:
<rule name="default page" stopProcessing="true">
<match url="^default\.aspx$" />
<action type="Redirect" url="{R:0}" redirectType="Permanent" />
</rule>
but it sent my browser in an infinite redirect loop. I amended it to the following and it worked fine:
<rule name="default page" stopProcessing="true">
<match url="(.*)default.aspx$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
Since you use IIS7, try this:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="default page" stopProcessing="true">
<match url="^default\.aspx$" />
<action type="Redirect" url="{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This is just a quick example, you can modify it to suit your need. Hope this helps!
This worked for me. http://www.w3schools.com/asp/met_redirect.asp
Using the above link, I wrote the following lines and it executed without any problem. Notice there is no semicolon at the end. My default.aspx page just contains the following three lines.
<%
Response.Redirect("~/portal")
%>
Possible reason is; usually language is mentioned at the top of .aspx pages or in the web.config file. If language is not mentioned, IIS uses VB.NET to compile the pages and we know that VB.NET doesn't uses semicolons to terminate the statements.
Use this code in your default.aspx page
<script language="javascript">
var url=location.href;
if (url.toLowerCase()=="http://www.doamin.com/default.aspx" || url.toLowerCase()=="http://www.doamin.com/#") {
window.location= "http://www.doamin.com";
}
</script>
精彩评论