views:

107

answers:

5
When user write http://nextech.pk/ in the browser, I want to redirect to http://www.nextech.pk/ , So I want to embend www to the URL when user write nextech.pk in the browser How can I accomplish this? I think I need to write some rules in the web.cofig to accomplish this, but I don't know exactly Thanks
A: 

just add index.html with:

<meta http-equiv="Refresh" content="0; http://www.example.com"&gt;
Yossarian
you mean that first the request goes to index.html and then transfer to http://www . nextech.pk/default.aspx?
Muhammad Akhtar
He means add that line to the default.aspx, not the index.html page.
Moayad Mardini
What if he has more than one page and someone arrives at a nested page without that statement?
Yuriy Faktorovich
added this but page refresh again and again and so on.......
Muhammad Akhtar
A: 

You might want to check with your domain name provider. You should be able to configure your hosting to automatically redirect from nextech.pk to www.nextech.pk.

But you could also do it in Global.asax or with an IHttpModule in the BeginRequest handler:

private void context_BeginRequest(object sender, EventArgs e)
{
    HttpApplication application = (HttpApplication) sender;

    if (!application.Request.Url.ToString().Contains("http://www."))
    {
        application.Response.Redirect(
            application.Request.Url.ToString().Replace("http://", "http://www."));
    }
}

Both of these methods would work with any request on your website.

jrummell
You cannot configure DNS to redirect - it has no knowledge of the HTTP protocol. DNS services which offer redirection listen on HTTP and then redirect your request. The action redirection will be done by a webserver.
Richard
My mistake. I changed DNS to hosting.
jrummell
+1  A: 

It will be there as an option in the domain settings page where your domain is registered. use nextech.pk or www.nextech.pk or both something like that.

Changing there will be the easy one.

Umesh
but I need both one, but want to redirect to www.nextech.com, gives the flexibility to the user whether he write nextech.pk or www.nextech.pk
Muhammad Akhtar
I am using DreamHost. And I have options like this in my cpanel under manage domains Do you want the www in your URL? Leave it alone: Both redcache .com and www.redcache .com will work.Add WWW: Make redcache.com redirect to www. redcache .com Remove WWW: Make www. redcache .com redirect to redcache.com Check whether you have some options like this in your panel.
Umesh
A: 

In the base page for your website or your Default.aspx file, check if the request url is prefixed with www. Otherwise redirect using Response.Redirect to the url prefixed with www.

Bjorn Bailleul
A: 

I don't do this at the ASP.NET level, as it would fail on static content such as images. Simply add an additional website to IIS, with the host header value of all the URLs you wish to redirect FROM (e.g. example.com). On the "Home Directory" tab, click on "A redirection to a URL", and enter the URL you wish to redirect TO.

This allows you to enter http://example.com/foo.txt and end up at http://www.example.com/foo.txt

(NB. These instructions are for IIS 6, they'll vary slightly for IIS 7).

Richard
I have check this, but not working for IIS 7
Muhammad Akhtar
In IIS7, create the webtsite as normal (you'll still need to specify a directory, but it won't actually get used). Then click on your newly created website in IIS manager, and click on HTTP Redirect in the IIS section of the main page. From there you can specify where to redirect to.
Richard