URL redirecting in classic asp - asp-classic

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).

Related

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.

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

Web.Routing for the site root or homepage

I am doing some work with Web.Routing, using it to have friendly urls and nice Rest like interfaces to a site that is essentially rendered by a single IHttpHandler. There are no webforms, the handler generates all the html/json and writes it as part of process request.
This works well for things like /Sites/Accounting for example, but I can't get it to work for the site root, i.e. '/'.
I have tried registering a route with an empty string, with 'default.aspx' (which is the empty aspx file I keep in my root folder to play nice with cassini and iis). I set RouteExistingFiles to false explicitly, but whatever I do when hitting the root url it still opens default.axpx, which has no code it inherits from, and contains a simple h1 tag to show that I've hit it.
I don't want to change the default file to redirect to a desired route, I just want the equivalent of a 'default' route that is applied when no other routes are found, similar to MVC.
For reference, the previous version of the site didn't use Web.Routing, but had a handler referenced in the web.config that was perfectly capable of intercepting requests for the root or default.aspx.
Specs: ASP.NET 3.5sp1, C#, no webforms, MVC or openrasta. Plain old IHttpHandlers.
Fixed my own problem: the issue is the integrated web server, Cassini or some such. Seems that it doesnt play nice with routing, and will by default simply return the default.aspx file or, if it is missing, show a directory listing.
Using IIS with a virtual directory works fine, but is annoying (frustrates code sharers because they need to set up new virtual directories when they open my app, and pollutes my own IIS instance. Bah. Probably what I'll do for the moment however, or setup a new application manually so I can use the domain host only path like what will exist in live.
An alternative is to use the updated version of cassini, seen here, which works if the default.aspx file is missing, but I have not worked out how to integrate it with visual studio yet. Any help would be appreciated, but its not a big priority given I have workarounds.
I realise that this is a really old post, but I just ran into the same problem using VS2012, so I'm posting this here just in case.
I solved the problem by installing IIS Express and setting the project to use IIS Express in Visual Studio. Solved the problem.

Changed a page from ASPX to HTM(L) but IIS points to ASPX regardless

We recently rewrote our company homepage and have come across a peculiar error. We have very few pages that need any code behind them, so we wrote the website in static HTML served out of IIS6. The few pages that need any code (Contact Us, with a contact form, for instance) are .ASPX pages.
The previous version of the website had more .ASPX pages, even if there was no code in the code-behind. One of those pages was "management.aspx", and in the new site this is, logically enough, "management.htm". We're smart enough to not just change the file extension -- we rewrote everything, it just has the name in common.
Here's the peculiar part: Even though every link in the entire website points to "management.htm", IIS6 continues to try and serve "manangement.aspx". I've reset IIS, stopped/started the Default Web Site under IIS6, deleted pages from "Temporary ASP.NET Files" and deleted out temporary GZIP files from the server as well. This isn't MVC or anything, so we have no explicit URL routing, and while I can see us having to implement static file handling in our web.config httpHandlers, I can't imaging that being a necessity.
What gives? Why is IIS6 still trying to serve the old "management.aspx" page when we're explicitly asking for "management.htm"? What can I do to fix it?
Your bindings have to be off somewhere... check top level for *, *.htm, managment.htm, etc. Then check virtual directories.
If you paste "http://yoursite/management.htm" in your browser and you get a YSOD, this is an IIS issue for sure.
Check if you still have the bin folder in the root of your site.
Do you get a 404 because it tries to load management.aspx and it's not there?
Check the Default Page on the Website/Virtual directory in Question, and if it exists remove the reference to manangement.aspx.

Why would an aspx file return 404 ("The page cannot be found")

Why when I access an aspx (e.g., http://www.example.com/foo.aspx - not the real site) through IE6 would I get a 404 Error (i.e., "The page cannot be found") in IIS6
I've got scripts enabled for the website and I've tried with executables enabled as well.
Here is the full error:
The page cannot be found
The page you are looking for might have been removed, had its name changed, or
is temporarily unavailable.
------------------------------------------------------------------------------
Please try the following:
Make sure that the Web site address displayed in the address bar of your
browser is spelled and formatted correctly.
If you reached this page by clicking a link, contact the Web site
administrator to alert them that the link is incorrectly formatted.
Click the Back button to try another link.
HTTP Error 404 - File or directory not found.
Internet Information Services (IIS)
------------------------------------------------------------------------------
Technical Information (for support personnel)
Go to Microsoft Product Support Services and perform a title search for the
words HTTP and 404.
Open IIS Help, which is accessible in IIS Manager (inetmgr), and search for
topics titled Web Site Setup, Common Administrative Tasks, and About Custom
Error Messages.
I can get to Default.htm in the same directory, so I know the path is right. I've opened it up to everyone (temporarily) so I know the permissions are right.
It could be a lot of things. I had this issue today because .NET had not been re-initialized after installing IIS (aspnet_regiis -i -enable or equivalent).
Check that the anonymous user under which the site runs has read access to the file foo.aspx.
IIS6 and later uses a 404 response, thereby not letting an attacker know whether such a file even exists.
I just happened to find another culprit for this issue. My foo.aspx page referenced a particular master page that had a <%# Register %> directive to a user control that did not exist. Removing the reference to the non-existent user control caused my foo.aspx to load instead of 404.
I found a solution here.
The real catch was using this:
Response.TrySkipIisCustomErrors = true;
The site is pointing to a different directory where the page is not.
It could be permissions, however I would think you would get an access error instead.
I'm assuming you are running IIS.
Check that www.example.com is going to the site that you think it is.
If you are hosting multiple sites on the same IP using host headers you may want to double check the name you are using is going to the site you think it is.
Ray and Joe probably have it. In order to serve any file type, IIS has to have a mapping for it. Aspx files require that they be mapped to the AspNet ISAPI dll, which the .Net installation normally takes care of. If you install IIS after .Net (and I'm sure there are other situations), you have to initiate this yourself by running aspnet_regiis.
ALTERNATE SOLUTION (same error perhaps different cause).
I had installed Visual Studio 2008 Pro without SQL Express it, and it caused this same error. Reinstallation of VS2008 with sql express included seemed to have corrected the problem, or perhaps the install took other actions. I did try to register ASP.net numerous times prior but no luck however it is definitely the most probable cause Just posting my experience for those pulling their hair as I was..
Thanks
If you register the .NET 4 version of IIS, you may find it's grabbed the registration of the aspx extension. If ASP.NET v4 is prohibited then 404 will be returned
I had this issue where some customers were reporting the 404.0 and some didn't have the problem at all(same page). I was able to navigate to any of the pages with no problems from my machine. Some customers would refresh and it would go away. I am using .Net 4.5.2 and IIS 7.5.
Looking at the IIS log file I would see:
sc-status sc-substatus sc-win32-status
404 0 2
sc-status.sc-substatus: 404.0 - Not Found
sc-win32-status: 2 - ERROR_FILE_NOT_FOUND
https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
https://en.wikipedia.org/wiki/HTTP_404
I found the problem was I had deployed a new version of the website in which the old version of the website had RouteConfig.cs/FriendlyUrlSetting setup by creating a project using the web forms template. The new version was created using an empty template. So obvious to me now.. no URL routing. Customers had a cache issue with certain pages on their machine(no .aspx extension) and having them clear browser data ultimately fixed the problem.
I got this issue when I tried using a different drive to host my apps. I ended up moving them to the wwwroot folder because it was working there and I did not have to time figure out why it is not working on the E:\ drive.
I had bin\roslyn compiler missing. Adding that all worked fine.
Check for double quote errors. I started getting a 404 on a single page because I accidentally had this:
<asp:TemplateField HeaderText="ImageURL"">
instead of this:
<asp:TemplateField HeaderText="ImageURL">
For an aspx page, error 404 can be quite misleading! I have seen all the answers and they presuppose assuming various issues with the file, page, path, etc. but the simplest issues is the fact that if there is an error in your asp page (i.e bad format, improper usage of control, etc. asp will think the page does not exist and will post a 404 when in all actuality, it is easy to ascertain if there is a bad format by simply clicking on design mode. If the page does not render no need to do anything else but look at what is causing the render error, fix and viola'! Your page shows since it was never missing or can't be found, but it simple did not know how to display! Too often people go looking for the wrong solutions and waste so much time! Hope this helps somone. :-)

Resources