First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Jeffrey F. Hill | Website: http://www.flash-db.com

Apache examples and exercise's

Exercise: Changing the default directory page.  Normally the default directory page is index.html.  As an intro to using the httpd.conf file where going to change the default page apache serves when a directory is requested to something else.

Open up the httpd.conf file (Located: C:\Program Files\Apache Group\Apache2\conf\httpd.conf) with Notepad - then do a search for index.html.  (Any line that starts with the # symbol in the configuration file is a Comment).  This should bring you to a line of code that looks like this:  

DirectoryIndex index.html index.html.var

Change this to DirectoryIndex MyNewIndex.html Now whenever you request a specific directory - this will be the page Apache will first look for.  You can add additional default pages to that and Apache will look for those pages in that order, personally I like to change this to DirectoryIndex index.php index.html index.htm.  (This is usually never needed - although it's nice to know how to do).

Exercise: Using .htaccess files.  .htaccess files are like small parts of the web server configuration.  You can place these file's in specific directory's to override the settings in the main httpd.conf file and to add special access controls and functionality to that directory.  .htaccess files are valid for all folders that are contained within the you've placed it in.  To allow the use of .htaccess files we do however have to change one part of the httpd.conf file.  Open up that file with Notepad now and change the following line:  (search httpd.conf for 'AllowOverride' to find the line faster).

AllowOverride None  To  AllowOverride All (This allows us to use .htaccess files in any directory).

Example 1:  Creating directory specific DirectoryIndex files.  Use notepad or a similar text editor to create a file called .htaccess and place that file in a directory/folder you want this to be valid for. Inside this file place the following;

DirectoryIndex MyMainPage.html 

This will make it so that if you visit this directory (folder) such as http://localhost/MyFolder/ - the default start page will be MyMainPage.html.

Example 2: Creating Custom error pages within a specific directory.  Follow the above steps to create a .htaccess file and add the following to that file;

ErrorDocument 404 http://www.domain.com/404.html
ErrorDocument 401 http://www.domain.com/401.html
ErrorDocument 500 http://www.domain.com/500.html

This just creates custom error documents and tells the server what to do and where to go if it finds an error such as your page not being found.  You can also specify default error pages in the main httpd.conf settings. However those settings will be valid throughout your site and not just in one directory.

Example 3:  Creating a password protected Directory.  This gets a little tricky.  But is one of the most useful examples of the .htaccess file for our purpose's.  Create an .htaccess file as before with the following and place it inside any directory you want to password protect.

AuthName "restricted stuff"
AuthType Basic
AuthUserFile C:\Passwords\users

require valid-user

You will also need to create a file that contains a UserName and a Password.  This file will be called (for this example) users (with no file extension) and placed in a folder we just created called Passwords at C:\Passwords\. Their are two basic ways you can create the UserName and encrypted password 1 way is using the PHP crypt() function, the second is using the htpasswd program that comes with Apache.  We will be using the second method.  

To create this file you'll have to open a Dos prompt and CD (Change directory) to CD C:\Program Files\Apache Group\Apache2\Bin\ Then run the following command:

htpasswd -bc users Jeff mypass  (the format is -bc NameToCallFile UserName Password).  This will create a file called users in the Bin directory.  The contents will look similiar to:  Jeff:$apr1$aq2.....$OtFywTSs9g9dBsnFlTRTj/  (The first part is the userName the second part after the colon is the encrypted password). Copy and paste this file into the C:\Passwords\ folder we created just a bit ago. 

When you try to access the Folder/Directory you placed this .htaccess file in with a web browser you will be prompted to enter a UserName and Password.  Enter in the UserName and password you used to create the file and you'll be allowed in. 

Overall it's not that important or secure to use this type of directory protection on Windows (cause it's still windows after all) and where mainly going to be using this as a local testing area.  If you allow access to your server by others from all over the internet it will still help in keeping specified directory's contents private and with controlled access.

That's about it for the Apache installation.  By locating your IP address that your computer is currently using for it's connection to the internet, you'll be able to give that address to others so they can view your website running on your own personal computer.  Since most ISP's such as cable and DSL providers change your IP address all the time you'll either have to keep telling others what your new IP is - or sign up for a service which constantly updates each time your IP changes and points it to a specific domain name for you.  If you are planning on letting other's visit your website running on a server on your home computer it's also a good idea to keep your firewall going - you'll have to change some settings so that it allows others to view your site.  

Next it's onto PHP..

» Level Advanced

Added: 2002-05-08
Rating: 9 Votes: 108
(10 being the highest)
» Author
Jeffrey Hill is a freelance web developer from Boulder, Colorado. He specializes in creating and developing dynamic database driven Flash content, applications, and content management systems. Specialty's include SQL, PHP, Perl, XML, web services, and Flash clients for web services.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.