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
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.
just add index.html with:
<meta http-equiv="Refresh" content="0; http://www.example.com">
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.
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).
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.
Related
I developed a website in asp.net and hosted on windows server which does not provide me the wild card entry for the subdomain name. How can I write my url as e.g
http://subdomainName.DomainName.org?
I want to redirect the url with a subdomain to the main domain; so url "subdomainName.DomainName.org" should redirect to "DomainName.org", where my subdomain name is not fixed. The subdomain will be assigned to each user.
How can I achieve this?
The subdomain is part of the DNS server, and work together with the IIS setup.
So, you can NOT change the DNS setup from asp.net, neither the IIS setup. When a provider gives your the access to add extra sub-domains, what is actually do is that create new entries on the DNS entry, and then add map that to the IIS, so that sub-domains to look at your site. If your provider did not have give you a tool to add sub-domains, nether you can edit the DNS entries, then you can NOT add them from asp.net.
If you can add sub-domains then you can manipulate what you going to server and show on global.asax at Application_BeginRequest using the redirect or the Rewrite the path. For example:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
var SubDomain = GetSubDomain(HttpContext.Current.Request.Url.Host);
if(!String.IsNullOrEmpty(SubDomain) && SubDomain != "www")
{
Response.Redirect("www.yourdomain.com", true);
return;
}
}
// from : http://madskristensen.net/post/Retrieve-the-subdomain-from-a-URL-in-C.aspx
private static string GetSubDomain(Uri url)
{
string host = url.Host;
if (host.Split('.').Length > 1)
{
int index = host.IndexOf(".");
return host.Substring(0, index);
}
return null;
}
Similar posts:
How to remap all the request to a specific domain to subdirectory in ASP.NET
Redirect Web Page Requests to a Default Sub Folder
How to refer to main domain without hard-coding its name?
Retrieve the subdomain from a URL in C#
How can I create a subdomain in an asp.net C# application? I am working with an asp.net portal.
I have my site which redirects all *.domain.com calls to domain.com. What I want to acheive is that first when the user enters a dynamic subdomain name he should be directed to its home page like if user writes www.microsite1.domain.com, then the site should point to page ~/Microsite/Home.aspx?value=microsite1, and when the user accesses www.microsite1.domain.com/about.aspx then i should able to get the argument value1=about.
The first step is to place the subdomain name in the DNS Host Server. To do that you need to manipulate the dns files. For example if you use BIND as DNS server you go and open the text file that keep your DNS configuration eg: "c:\program files\dns\var\mysite.com" and there you add a line as
subdomain.mysite.com. IN A 111.222.333.444
Also you change the ID of the file to give a message to BIND to update the subdomains.
Second step is to redirect the new subdomain to the correct directory. You do that on the protected void Application_BeginRequest(Object sender, EventArgs e) on Global.asax using rewritepath
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.Host.StartsWith("subdomain."))
{
// here you need to find where to redirect him by reading the url
// and find the correct file.
HttpContext.Current.RewritePath("/subdomain/" + Request.Path, false);
}
// .... rest code
}
Its not so easy, not so hard... maybe there are some more minor issues like permissions to write to dns. Also you need to know dns, read the manual about.
Occasionally, my IIS 6 server will receive a request which contains a space after ".aspx", like:
http://www.foo.com/mypage.aspx%20?param=value
The "%20" immediately following ".aspx" causes the server to result in a "404 Page Not Found".
Is there a way to configure IIS to accept ".aspx%20" and process the page as if the "%20" didn't exist?
I looked at the "Home Directory" / "Configuration" in the properties of the site in IIS Manager and I added an entry for ".aspx%20" but that didn't work. Any other suggestions are appreciated.
+1 for the custom HttpModule (as Frédéric Hamidi suggested). It's a clean, modular solution and may help you rewrite other URLS, should you need to do so.
Your OnBeginRequest (referring to the link Frédéric provided) might look more or less like this:
private void OnBeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
string url = context.Request.RawUrl;
context.RewritePath(url.Replace(".aspx%20",".aspx"), false);
}
You might want to consider writing an HTTP module to remove the trailing space from the URL.
Override the 404 page in your web.config and handle the situation you described in code.
I thought it should be a simple straight-forward matter but after struggling with it for too much time I'm gonna have to seek for help.
What I need is to redirect all requests for my web application that match the following pattern - "^(http://[^/]+/blogs/[^/.]+)/?$" to the path of "$1/Default.aspx".
(Using English rather than Regex: http://myapp/blogs/randomdirthatdoesntexist -> http://myapp/blogs/randomdirthatdoesntexist/default.aspx)
The sub-directories in "blogs" do not physically exist, instead a 3rd party product deals with the requests to randomdir/Default.aspx", but when you go to "randomdir/" you get 404 not found, which is what I'm trying to fix.
I tried to use global.asax and also HttpHandler, but I couldn't get either of them to fire up on the requests to those 404 paths.
So, in short, what should I do to redirect paths of non-existing directories?
Thanks.
I accomplished something similar to this by setting the Error Page for 404s in IIS to a page you create. This page is able to access the page which is being requested, and perform some additional logic. So the 404 for randomdirthatdoesntexist/ still happens, but the 404 handler notices this and redirects to randomdirthatdoesntexist/default.aspx.
I dug out my old code and tweaked it a little to do what you need in an over-simplified fashion, but I don't have IIS6 anywhere to test:
protected void Page_Load(object sender, EventArgs e)
{
String sQueryString = Request.RawUrl.Substring(Request.FilePath.Length);
Regex rx = new Regex(#"^\?(?<errorcode>\d+);(?<args>.*)$", RegexOptions.ExplicitCapture | RegexOptions.Compiled);
Match m = rx.Match(sQueryString);
if (m.Success)
{
if (m.Groups["errorcode"].Value == "404")
{
String sArgs = m.Groups["args"].Value;
Uri requestedUri = new Uri(sArgs);
// You can now check the value of uri and perform redirection...
if (requestedUri.ToString().EndsWith("/"))
{
Response.Redirect(requestedUri + "Default.aspx")
}
}
}
Response.StatusCode = 404;
}
Use a Custom HttpModule, or Http Handler, which sits in early enough in the Http Request pipeline to direct your requests appropriately before any possible 404 response is determined.
See my own SO Q&A; Server.Transfer or Server.RewritePath for more details.
I need to move all requests from one domain to another. I want to change part of URL, like subdomain.olddomain/url -> subdomain.newdomain/url.
I was sure that this is piece of cake and wrote Application_Begin request as:
void Application_BeginRequest(object sender, EventArgs e)
{
string url = Request.Url.ToString().ToLower();
string from = ConfigurationSettings.AppSettings["from"];
if (url.IndexOf(from) >= 0)
{
url = url.Replace(from, ConfigurationSettings.AppSettings["to"]);
Response.Redirect(url);
}
else
{
if (url.IndexOf("error.aspx") < 0)
{
Response.Redirect("Error.aspx?url=" + Server.UrlEncode(url));
}
}
}
So far, I forget, that BeginRequest started only when file physically exist.
Any ideas, how I can make such redirect in asp.net without creating hundreds of old pages?
Not 100% sure, but I think if you uncheck the Check that file exists option in IIS, it should work. How you do this depends on the IIS version.
I would recommend using a tool like ISAPIRewrite [http://www.isapirewrite.com/] to manage this for IIS 6, or the built in URL Rewriting for IIS7.
No reason to reinvent the wheel...
I believe you can specify an ASPX to run on 404 errors. That page can perform the redirect.
I would recommend doing this on the DNS level. I would redirect with a permanent 301 redirect to ensure that your search engine rankings are not affected.