IIS7 URL Rewrite with dynamic subdomains - asp.net

My goal is to implement the following scheme using the subdomain and path as attributes:
Use a wildcard DNS entry that routes any subdomain to the root site:
Example:
*.example.com
ex: http://xyz.example.com
to
http://example.com
Next I want to rewrite the requests to point to a specific page, passing both the subdomain and the request path as attributes.
Example:
http://xyz123.example.com/images/header.jpg
to
http://example.com/get.aspx?id=xyz123&path=/images/header.jpg
I've seen several questions on here regarding similar goals, but not quite the same. I'm new to using rewrite rules, so any help is appreciated. I will update this as I make progress.

for IIS7, url rewrite functionality is built-in. Rules are set in web.config. For IIS6 you need an ISAPI dll that does the same for you. Use IIRF, it works just fine.

Related

Specify robots.txt using IIS for different sub-domains

I have a site at say www.example.com and also have two staging platforms at beta.example.com and preview.example.com and I need a way to set different robots.txt for each using IIS or something similar
The reason for this is that I want to disallow spiders on all but the www domain as they are spidering duplicated content
Anyone know if this is possible?
Is there a reason you can't just have 3 different robots.txt files, one for each host?
If you need to handle this automatically, I would suggest a HTTPHandler that handles requests for robots.txt. If the Request.Url.Host is www.example.com, return robots.allow.txt, if not, return robots.deny.txt.
If you are interested in this idea and need some example code to get you started, let me know.

URL aliases: rewrite, routing, not sure

ASP.net 4.0 / IIS7.
I want to "alias" a single web form to appear as various extensionless urls. Given a form in my web root called "mySite.com/ColorWebForm456.aspx":
I want it served as multiple names, e.g., mySite.com/Color, mySite.com/Colour, mySite.com/Colors, mySite.com/Coler, etc., without creating folders and duplicate forms with those names.
I never want mySite.com/ColorWebForm456.aspx displayed in the browser, it needs to display as mySite.com/Color, even if the user somehow acquires it and types in that exact ~.aspx address.
The variations will account for several alternate or mis-spellings users might attempt - I don't want them "corrected", however. So, if a user types in mySite.com/Colour, the url is NOT rewritten to mySite.com/Color, but the same page is served via ColorWebForm456.aspx as the requested "mySite.com/Colour".
I've seen so many articles on this that I'm not even sure where this would be best handled: in Global.asax, IIS7 URL Rewrite, web.config, etc., and I'm not even sure this is technically a case of url rewriting or routing... ?
You can achieve this with ASP.NET 4 routing for web forms, or if you are using MVC its available too: http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
Except potentially for #3; it would work, but you would have to explicitly declare the variations you want. In that case, rewriting supports regular expression matching, so that might be better for what you are looking for.
I had issues setting up URL rewriting and various aspects of web forms, so be aware. There is some setup required for a rewrite module potentially (I could have done it wrong too), where URL routing is already built in and handled.
HTH.
If you have a rewrite engine available:
(using ISAPI Rewrite .htaccess syntax)
# redirect any requests for the filename back to the friendly URL
RewriteRule ^/colorWebForm4567.aspx(.*) /Color$1 [NC,R=302]
# rewrite /Color requests to the web form
RewriteRule ^/(Color|Colour)/(.*) /ColorWebForm4567.aspx$2
Following article expalins it all, but if you will be using a web host after you complete your application I would check with the hosting provider first to see if they are offering everything you need to have:
http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
Hope this helps!
As a side note: Search engines dislike it, if you publish content under different URLs ("duplicate content").
So my recommendation would be to stick with one (rewritten) URL, not multiple (rewritten) URLs for the same content.

subdomain rewriting in ASP.NET?

How do i do this in .NET? http://why.does.my.head.asplode.net/ I want to do something like ytmnd where each url is a different user generated page. I might need something as simple as pointing to a directory so dirname.mysite.com will redirect to http_public/userGenContent/dirname/
I suggest you use UrlRewritingNet. You may have to add a rule similar to this:
<add virtualUrl="http://(.*).asplode.net/index.aspx"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/Users.aspx?username=$1"
ignoreCase="true" />
I cannot test this since I'm on a Linux machine right now, but it should work ;).
HAHA ...
I handled the Begin request event in global.aspx for this because my urls come from sql server.
Why are so many people sub domain crazy these days?
in Begin Request you can do something like this (not usre exactly because I aint got it in front of me right now) ...
request.rewriteurl( "new url" );
... this will take whatever the source url is and "redirect" without a redirect the response to a url you can use internally.
The various UrlRewriting tools will help you once you have the request getting into ASP.NET, however before that happens you need to setup IIS to pass these requests to your app. This is simple enough if there's only one app on the server, but more difficult otherwise.
Check out http://msmvps.com/blogs/bernard/archive/2005/03/22/39218.aspx for some details on wildcard subdomains on IIS.
Alternatively use an IIS level rewriter like ISAPI Rewrite
Essentially search for IIS wildcard subdomain to find a wealth of approaches.
Well, IIS7 has built url rewrite functionality. You can specify rules etc in web.config. But for IIS6 you need ISAPI dll that does the same for you. I've used IIRF and it works just fine.

Url Rewrite of Host in ASP.NET on IIS 6

I know this question is overly popular, but I find nothing that addresses rewriting the host, only rewriting the path.
How could I accomplish redirecting/rewriting "http://myHost.com" to "http://myAlias.com/hidden/default.html"?
ADDED: where myAlias is just an alias of myHost.
Quick and dirty ... Create a default document for the site (default.aspx for example) to redirect to /hidden/default.html
Along the lines of what James had mentioned with the default.aspx in your root. Then you could rewrite simply..
HttpContext.Current.RewritePath("http://myHost.com/hidden/default.html");
You can use an ISAPI-filter in IIS, like Helicon to rewrite the document path:
http://www.isapirewrite.com/
The lite version is free
Edit:
Ok, your want to redirect to another domain. I don't think that Helicon supports that.
You can try (c#):
HttpContext.Current.Response.Redirect("http://myAlias.com/hidden/default.html");

Subdomain to point to subdirectory of main site?

I have my website running at:
www.example.com
I also have this other domain:
www.example.net
Is it possible to point "www.example.net" to a subdirectory of "www.example.com"? For example:
www.example.net-> www.example.com/other
?
It's not just a redirect, I need to serve the pages and see "www.example.net" in the user's browser.
What do you think?
We use IIS7 and everything is ASP.NET (aspx pages, ascx controls, etc).
Using ISAPI Rewrite
[ISAPI_Rewrite]
RewriteCond Host: (?:www\.)?other\.com
RewriteRule (.*) http://www.domain.com/other$1 [I,L]
You're looking at two methods for this:
URL rewriter
Server.Execute method
The first is a httpmodule that silently redirects the request without changing the url in the address bar, the second is a method you can call within aspx code. I think the latter is only good though if you're working in the same application, so I would go with the URL rewriter.
There is an official iis7 URL rewriter available on www.iis.net afaik.
-Oisin
Two questions for you:
Can you set them up as two different web sites in IIS?
If someone visited example.net/page.htm, would that be the same as example.com/other/page.htm?
If the answer is yes to both of those, why not just setup a new web site in IIS and point it to the "other" file path.

Resources