WWW FAQs: How do I switch to ASP without breaking all my links?


2007-02-13: "I want to use ASP in my web pages. I'd like to add ASP commands to my existing HTML pages, or possibly switch from PHP to ASP. But won't that break all of my existing pages?"

Not at all! You can do it without rewriting all of your links... or revealing the fact that you use ASP. The trick is to convince your web server to look for ASP commands inside files that have file extensions other than .asp.

Since you are interested in using ASP, I'll assume you are using Microsoft Internet Information Server. That's highly likely because ASP on any other web server is extremely rare.

Microsoft Internet Information Server (IIS) allows you to decide what "script interpreters" are used to respond to each file extension. In English, that means you can tell IIS exactly what to do with a filename that ends in .htm. You're not stuck treating that file as an ordinary static web page. You can configure IIS to handle it in a very different way.

So to convince IIS that .html, .htm, and .php pages should be scanned for ASP commands, all we have to do is use the Internet Information Services control panel to change the way these file extensions are handled.

It's easy! Just follow these steps (I assume you are logged into the web server's desktop as an administrator):

1. Start -> Control Panel -> Administrative Tools

These steps are correct for the Windows XP "Classic View." If you are using the standard view, the steps are slightly different:

Start -> Control Panel -> Performance and Maintenance -> Administrative Tools ->

We recommend the "Classic View" for those who are running websites on their PCs.

2. Double-click Internet Information Services"

3. Drill down in the left-hand "tree," clicking on your server's computer name and then on "Websites," to locate your website (or "default website" if you have only one)

4. Right-click your website (or "default website" if you have only one), and pick "properties"

5. Click on the "Home Directory" tab

6. Click "Configuration..."

7. In the "Application Settings" area you'll see a list of the file extensions that are handled as ASP and as various other special scripting languages

8. Select .asp from the list

9. Click "Edit." We're not going to make changes here, we just want to copy the settings so we can apply them to other types of files

10. Write down all of the settings you see, especially "Executable"

11. Click "Cancel"

12. Click "Add"

13. Set "Extension" to .htm (note the dot is required)

14. Set all of the other fields the same as for .asp (you did write it all down... right?), especially "Executable" (the path shown may get contracted after you edit it, do not be concerned, it's just to make it more readable)

15. Click "OK"

16. Repeat these steps for .html

17. Optional: repeat these steps for .php, if you want to use ASP in files with a .php extension. (I would go the other way 'round and migrate to a free web server on a free operating system, but it's your computer!)

18. Click "OK" to return to the website properties window

19. Click "OK" to finish the job!

But Did It Work?

Good question! To find out, fire up Windows Notepad and create a file called mytest.htm in your web server's home directory. In that file, insert only the following code:


<% Response.Write("Hello World!") %>

Now, access the page with Internet Explorer or another browser. The page should be at this URL if you are logged into the server itself:


http://localhost/mytest.htm

If you see ONLY "Hello World!" then everything is working great! The ASP code executed, even though the file has a .htm extension.

If you see all of the code, including the <% and %> marks, then you didn't do something quite right. Go back and follow the steps above more carefully.

If you get a "not found" error, you are not at the desktop of the server, or you have placed mytest.htm in the wrong directory, or you have named the file incorrectly (mytest.txt is not correct).

You need to do this test by fetching the page from the real web server you just reconfigured— opening the file locally on your hard drive doesn't involve the web server, so ASP doesn't run. But you knew that... right?

Conclusion

Upgrading your pages to take advantage of ASP doesn't have to mean big changes in your existing HTML. Armed with this technique, you can seamlessly move to including ASP commands in your existing pages.

There is one catch: checking every file for ASP commands does take some CPU time. And if your site is popular, that can have a significant impact on your site. If you experience performance problems, consider not using ASP for the most popular pages, unless they truly require it. As for pages that do require ASP, you can configure IIS to redirect users from the old .htm versions of such pages to new .asp versions. For more information about this approach, see the article how do I redirect the user to a new page?

"What about migrating to .aspx (ASP.NET)? How do I do that?"

The same techniques work just fine for ASP.NET pages. Just copy the settings for the .aspx extension instead of those for the .asp extension.

Legal Note: yes, you may use sample HTML, Javascript, PHP and other code presented above in your own projects. You may not reproduce large portions of the text of the article without our express permission.

Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!