subdomain rewriting in ASP.NET? - 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.

Related

URL redirecting in classic asp

My application url is like this.
http://somename/webname/default.asp
It also contains some other default.asp like http://somename/webname/booking/default.asp and some others too. Even if user tries to access other default.asp, I want them to redirect to
http://somename/webname/default.asp.
Update:
I have been checking like this in global.asa.
Application("txtPathInfo") = Request.ServerVariables("PATH_INFO")
if (Application("txtPathInfo") <> "/webname/default.asp") then
Response.Redirect("/webname/default.asp")
end if
But the problem is, first time it is working fine,redirecting to desired page. Next attempt it allows, may be the global.asa is not called while trying to access in same session. Please anyone provide some feasible solution on this. Thanks :)
One quick way is to put this code in a default.asp page in each possible folder.
<%
Response.Redirect "http://somername/default.asp"
%>
If there are too many folders or some of those folders don't physically exist you'll need to use a url rewrite rule or code within a custom 404 error page.
For rewrite rules this will depend on which version of IIS you are running. If IIS6 then I'd recommend IIRF but if you're running IIS7 or IIS8 then use the built in url rewrite module (installed via Web Platform Installer).

Change the broswer's address name

how can I change the broswer's address name?? For example I have this: www.example.es/Pages/site.aspx and I want this: www.example.es/site
How can I do that?? Ty.
you can use the 404 error challenge and Server.transfer() to do URL rewriting using classic asp
Things you need
How to create a custom 404 page
Regular expression pattern to recreate actual file path or a predefined list
eg:-www.example.com/pg1/ ---> /pages/pg1.asp
Simple server.transfer() to handle valid / invalid server requests.
<%
IF Request.ServerVariables("SCRIPT_NAME") = "pg1" THEN
Server.transfer("/pages/pg1.asp")
ELSE
Response.write("404 - Page not found");
END IF
%>
that's all you are doing basic level URL rewriting using classic asp.
Your question asks about .aspx (asp.net) pages, but your question is tagged asp-classic... you will get better responses to future questions by using the correct tag.
In this case: as long as you can install a module on your server, and you're server can process ASP.Net's web.config files, you can use the IIS URL Rewrite module that Graham mentioned in the comments. I've got an old ASP-Classic site that uses the module just fine.
If you can install an extension, you could also use Ionics Isapi Rewrite Filter. Some hosts have this installed already.
All things being equal, if you have the ability to run your choice, I'd go with the IIS URL Rewrite Module as I've generally seen better performance from it. But that may just be me.
If you do not have that kind of access to the server, and there's not something already installd, you may have to go with the 404 mapping Jeevaka Nuwan Fernando mentions.
There are some other options, but they depend on if you are using ASP.Net or ASP-Classic.

How to get Umbraco to handle requests for non .aspx files (IIS integrated pipeline mode)?

I'm trying to get the 301 URL Tracker package for Umbraco to work to my likings.
My goal is to be able to map the old URLs (from another CMS) to the new Umbraco URLs. In my specific situation, the old site is PHP based and therefore use the .php file extension (http://example.net/test.php -> http://example.net/test/) - but it could be any non .aspx extension (asp, png and so on). The problem is that Umbraco is not handling request for .php files. It works perfectly for .aspx and directories (extensionless URLs).
I have tried various things for getting this to work. Before I go any further, I should note that the Application Pool is in integrated mode and .NET 4.0.
I kind of got it to work by defining a custom error in the web.config:
<customErrors defaultRedirect="not-exists.aspx" />
This triggers the handlers defined in NotFoundHandlers in the Umbraco config file 404handlers.config. But has the side effect of returning a 302 Found header, before the 301 URL Tracker kicks in and handles the 301 redirect. And this is just a big SEO "no no".
I then tried to explicitly create a HTTP handler module for .php files. I successfully got the System.Web.UI.PageHandlerFactory module to handle the request for the .php file. But this does not invoke any of the NotFoundHandlers in Umbraco.
As I understand the integrated pipeline in IIS 7, all the modules registered should try to handle the request (http://stackoverflow.com/questions/3765317/how-to-catch-non-aspx-files-with-a-http-module). But perhaps somebody can enlighten me on this subject?
Others are also experiencing the difficulties in getting this configuration to work: http://our.umbraco.org/projects/developer-tools/301-moved-permanently/feedback/7271-when-the-old-pages-are-not-from-umbraco
What am I missing in getting Umbraco to handle request for non .aspx files in integrated pipeline mode?
If you are already running under Integrated Pipeline mode then the included UrlRewriting.net module should pick up requests automatically.
Simply add:
<add name="phptoaspx"
virtualUrl="^~/(.*).php"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="/$1.aspx"
ignoreCase="true" />
to your /config/UrlRewriting.config file, and all should be well.
P.S. You should not be using a customError handler to handle SEO 301/302'ing content. This can be a massive headache in terms of maintainability - please trust me on this, I tried this once when I was a junior .NET dev!
I am not familiar with Umbraco, but believe this is what you are looking for http://blogs.iis.net/ruslany/archive/2008/09/30/wildcard-script-mapping-and-iis-7-integrated-pipeline.aspx
Of course you'll have to add your own rewrite rules... so this only gets you half way.

Possible bug/issue in ASP.NET 3.5 related to Request.RawUrl property

I posted a query for 301-redirect using ASP.NET 3.5 here:
Redirecting default.aspx to root virtual directory
Based on the replies I got there, I realized there might be a bug in ASP.NET's Request.RawUrl method which is unable to return the actual raw url (without /default.aspx) when being used in a sub-directory, i.e. the /default.aspx page is inside a subdirectory.
Can someone please shed some light on this possible bug?
Thanks,
Asif
i found a good explanation here
http://codeasp.net/blogs/vivek_iit/microsoft-net/873/301-redirect-from-default-aspx-to-site-root
Thanks
If you suspect this is a bug, then the place to go is Microsoft Connect, where you can report and discuss the bug directly with Microsoft.
Edit: I was able to reproduce the look per your comments.
I was unable to reproduce the infinite loop, however. I injected code into the Global.asax Application_BeginRequest handler of a web application and got the expected behavior of a single redirect.
There are other, and IMO much better, options for handling global redirect rules. On IIS7, I use the URL Rewrite module to configure rewrite rules in IIS. You can read more about it and download it here: http://www.iis.net/download/urlrewrite. The appeal of a solution such as this is that you can customize and update your rewrite rules without recompiling the application.
Edit: I was able to retrieve the raw URL without the default.aspx (after the redirect) by using instead:
Request.ServerVariables["CACHE_URL"]
It's worth a shot.
Have you looked at the IIS settings for your virtual directory? If there is a default document set to default.aspx then this will explain the infinite loop that you are experiencing. You are telling the website to redirect to the virtual directory without the "default.aspx" and IIS is detecting this on the next request and putting it back in ad infinitum.
Right click your virtual directory, select Properties and then the Documents tab. If default.aspx is in the list then that is what you will get. The Url of the request will be passed to the ASP.NET worker process as /folder/default.aspx rather than /folder/
This is not a bug. If IIS didn't do this, you would get a page not found error.
Sounds to me like you need to investigate URL rewriting: http://msdn.microsoft.com/en-us/library/ms972974.aspx

How to 301 redirect

Ok, I need to know how to do a redirect (where to put the code or specify the setting). We're redirecting from one app to another after we've moved the app.
So for example, if a user goes to existing.example.com/archive/
we want to redirect any requests that contain old.example.com to new.example.com. The rest in the url stays the same. So for example /archive/ is one example so we'd want to redirect them to the new location of this app which is new.example.com/archive/
I need to figure out how to check to see if the incoming URL to our existing site has existing.example.com, and if so replace that part only with the new new.example.com and keep the rest of what's in the url
I know you can do this in IIS 7 or programmatically. I guess I'm not understanding how to do this in either situation. I installed the IIS7 Rewrite plugin and ok fine, but here's what I don't get:
Pattern:
RedirectURL:
I don't see how in that interface I am able to match the existing.example.com and then what to put in the RedirectURL because I want to put the entire URL with only that existing.example.com changed to new.example.com in for the REdirectURL... and I don't see how I'd do this in IIS 7.
Here's a post describing how to match one domain and redirect to another with everything else in tact using the IIS7 URL Rewrite add-in: http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx
You don't need any complicated rewriting stuff to do such a trivial redirect, it's a basic web server feature. In IIS7 you can now choose not to install it (from World Wide Web Services->Common HTTP Features->HTTP Redirection), but it would be unusual to do so.
Edit the ‘Bindings’ of the main web site in IIS Manager so that it only responds to the ‘Host name:’ new.example.com, then create a new web site bound to host name old.example.com. For this site hit the ‘HTTP Redirect’ option and ‘Redirect requests to this destination:’ http://new.example.com/ with ‘Status code:’ 301.
In config XML terms:
<system.webServer>
<httpRedirect enabled="true" destination="http://new.example.com/" httpResponseStatus="Permanent" />
</system.webServer>
In your site's global.asax.cs file you can redirect from BeginRequest as follows. You can write the routine to replace the domain names as needed with regex or string.replace() or whatever you prefer.
protected void Application_BeginRequest(Object sender, EventArgs e){
...parse urls...
Response.Redirect(myNewPath)
}
For asp.net applications, I've had good experience with urlrewriting.net, a free component where you can configure redirects in your web.config. It allows you to enter regular expressions and specify the new URL with back references, e.g. something like this (untested):
<add name="newdomain" virtualUrl="^http\://old.example.com/(.*)$"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="http://new.example.com/$1"
redirect="Domain"
redirectMode="Permanent"
ignoreCase="true" />
Drawback: This needs to be configured for every ASP.NET application (and you might need to reconfigure IIS to route everything through asp.net, or it might only work for .aspx files). If you want to redirect your complete domain, you are probably better off doing this on IIS level rather than on application level. For this, however, you might get better help on http://serverfault.com, which is where the sysadmins reside...

Resources