简体   繁体   中英

How to add www to url and redirect?

How can I response redirect from http://domain.com to http://www.domain.com ? Code, not Web.config, which doesn't seem to work for me.

If you are hosting in IIS, then you can set up a HTTP redirect.

IIS6 Redirects

IIS7 Redirects

Information about 301 redirects

EDIT

You could add the following to your Page_Load method:

// Check if page is running under theperfectfajita.com. If not redirect ...
if (!HttpContext.Current.Request.Url.Host.Contains("localhost"))
{
    if (HttpContext.Current.Request.Url.Host.CompareTo("domain.com") != 0)
    {
        HttpContext.Current.Response.Redirect("http://www.domain.com" + Context.Request.Url.PathAndQuery);
    }
}

Use a 301 redirect to the correct url. Something like this;

Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
Response.RedirectLocation = "http://www.domain.com";

I think this is the job of mass redirect or also know as mass 301 redirect. What it does, it transfers any URL to the destinied place where you want. It can be done via different ways, such as plugins if you use wordpress or any other cms, php with a code etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM