i am trying to solve www and non www url problem in windows based hosting,
i have done some work as given below , but i need to check whether it is correct or not , it will solve my problem or not.
web site pages build using classic asp
(shared) hosting on IIS 7.0 with go daddy
my global.asax coding as follows
void Application_BeginRequest(object sender, EventArgs e)
{
string newUrl = string.Empty;
try
{
if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().StartsWith("http://itcorner.org.in"))
{
if (HttpContext.Current.Items["UrlRewritingNet.UrlRewriter.VirtualUrl"] != null)
newUrl = "http://www.itcorner.org.in" + HttpContext.Current.Items["UrlRewritingNet.UrlRewriter.VirtualUrl"].ToString();
else
newUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Replace("http://itcorner.org.in", "http://www.itcorner.org.in");
Response.Status = "301 Moved Permanently";
Response.StatusCode = 301;
Response.StatusDescription = "Moved Permanently";
Response.AddHeader("Location", newUrl);
Response.End();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Thanks
Related
In order to prevent duplicity on my category pages I want to redirect those ending with / to without:
from - www.domain.com/category/
to - www.domain.com/category
I'm using standard web forms.
Please advise,
Thanks.
In global.asax file add following code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().ToLower().EndsWith("/category/"))
{
string sNewPage = Request.Url.ToString().ToLower().TrimEnd('/');
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", sNewPage);
Response.End();
}
}
I have designed my website in asp.net and hosted it on web server.
I want to redirect user to my site if user will type mydimainName.com on http://www.mydimainName.com.
If some one has solution then please let me know.
Thanks,
Munish
You'd want to redirect the user as soon as possible, so placing this in Application_BeginRequest of your Global.asax makes the most sense. You also likely want to do this as a 301 redirect for SEO purposes. Something like so is typically what I would do (not 100% tested)
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string sHost = Request.Url.Host.ToLower();
if( sHost == "mydimainame.com" )
{
Response.StatusCode = 301;
Response.Status = "301 Moved Permanently";
Response.AddHeader( "Location", "http://www.mydimainName.com");
Response.End();
}
}
You can try something like this in your masterpage or Global.asax:
string req = Request.Url.ToString();
string a = req.Substring(0, 10).ToLower();
if (a != "http://www")
{
// this is for localhost and my staging at discountasp,
if (a != "http://loc" && !req.Contains("discountasp"))
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", "http://www." + req.Substring(7, req.Length - 7));
Response.End();
}
}
In my asp.net mvc 3 application I've done some error handling and I happy it working. It works properly on the local computer. But on the hosting the iis perhaps substitute errors views by its own error pages. I think and I'm almost sure it's because of Response.TrySkipIisCustomErrors.
But I set Response.TrySkipIisCustomErrors to true; and there's no effect.
web.config
<customErrors mode="Off"></customErrors>
Global.asax
void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
Response.Clear();
Server.ClearError();
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Response.TrySkipIisCustomErrors = true;
var routeData = new RouteData();
routeData.Values["controller"] = "Errors";
routeData.Values["action"] = "ServerError";
var httpException = exception as HttpException;
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch (Response.StatusCode)
{
case 403:
routeData.Values["action"] = "Unathorized";
break;
case 404:
routeData.Values["action"] = "NotFound";
break;
}
}
IController errorsController = new ErrorsController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorsController.Execute(rc);
}
How rewrite this code to get IIS on the hosting handle errros as I want.
Try to derive from a controller base class, which will override the OnException method, and put all your error handling code there.
I have a app that is setup on IIS 6.0. We are having trouble with Search Engine optimization with the default.aspx page.
For Example when I type www.xxxxxx.com/default.aspx it should redirect to www.xxxxxx.com.
Can anyone please help me with this problem?
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (HttpContext.Current.Request.RawUrl == "/default.aspx")
{
Response.StatusCode = 301;
Response.Status = "301 Moved Permanently";
Response.RedirectLocation = "/";
Response.End();
}
}
}
}
Set the default document in IIS under the Documents tab. Once in the Documents tab, check "Enable default content page" and set Default.aspx as the first item (or only) in the list.
could u please tell me, how to make 301 permanent redirect in asp.net?
I have written code in Global.asax file but my web client says it is not working,
i have written follwing code in Global.asax file:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(
"http://lsatfreedom.com"))
{
HttpContext.Current.Response.Status =
"301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location",
Request.Url.ToString().ToLower().Replace(
"http://lsatfreedom.com",
"http://www.lsatfreedom.com"));
}
}
Is it useful?
Please help.
Thanks
First try to see if this redirection works in a page load.
If yes, then try it with Begin_Request.
Hope this gives you some clue:
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
}
I think you are missing Response.Clear() and Response.End(), Please try with this.
For example:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(
"http://lsatfreedom.com"))
{
string sNewPage = Request.Url.ToString().ToLower().Replace(
"http://lsatfreedom.com",
"http://www.lsatfreedom.com");
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", sNewPage);
Response.End();
}
}
I believe you are missing a CompleteRequest()
So your code should look like this:
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(
"http://lsatfreedom.com"))
{
HttpContext.Current.Response.Status =
"301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location",
Request.Url.ToString().ToLower().Replace(
"http://lsatfreedom.com",
"http://www.lsatfreedom.com"));
CompleteRequest();
}
If you don't add the CompleteRequest, then ASP.Net will try to handle it itself, in which case the header may exist, but the Status may actually be overwritten between beginning the response and ending it. This would make it so that you don't get an actual redirect.
I would change the web.config and add the following rule from this answer.
Forwarding http://mydomain.com/ctrlr/act/val to http://WWW.mydomain.com/ctrlr/act/val
That is how we are adding the www