Working With Apache’s Alias Directive

July 7th, 2008

Apache Directory Alias TutorialA couple of weeks ago, I’ve post a series of tutorials on How To Install Your Own Web Server on Windows with Apache, PHP and MySQL. Today I’m going to sort of pick up where these tutorials left by introducing you to Apache’s Directory Aliases.

While it may not be necessary for you to read all the previous posts, they may provide you with a better understanding of Apache’s HTTPD Server.

An Introduction To Apache’s Alias Directive

From Apache’s HTTPD Server perspective, a directory alias allows documents to be stored in the local filesystem other than under the DocumentRoot. This is set using the Alias directive. So for example, you could have the following directory structure:

  • c:\www\mywebsite – The document root of your website
  • c:\downloads – Contains downloadable files

In this case, the content of c:\www\mywebsite will automatically be published on the web as it is your website’s root folder by default (ie.: http://mywebsite/). On the other hand, c:\downloads will not be available as it sits outside of c:\www\mywebsite.

Let’s see how we can make the content of the c:\downloads available publicly as if it was a subdirectory of c:\www\mywebsite. And not only do we want it to be available publicly, we want to access it as if it were named files and not downloads. So after we’re done, we should be able to access the content of c:\downloads through this URL:

http://mywebsite/files

You following me? Great.

For this tutorial, I am using a WAMP setup which means Windows-Apache-MySQL-PHP. Apache’s configuration files are located in C:\Apache2\conf. My website VirtualHost entry in c:\Apache2\conf\httpd.conf actually looks like this:

NameVirtualHost *:80


    ServerAdmin info@thewebhostinghero.com
    DocumentRoot "C:/www/mywebsite"
    ServerName mywebsite
    ErrorLog logs/mywebsite-error_log
    CustomLog logs/mywebsite-access_log common

First we’re going to add an Alias directive to our VirtualHost entry:

NameVirtualHost *:80


    ServerAdmin info@thewebhostinghero.com
    DocumentRoot "C:/www/mywebsite"
    ServerName mywebsite
    Alias	/files	"C:/downloads"
    ErrorLog logs/mywebsite-error_log
    CustomLog logs/mywebsite-access_log common

Then we need to specify the permissions for this directory. We’re going to add a Directory entry right after the VirtualHost one:


  Options +Indexes
  AllowOverride None
  Order allow,deny
  Allow from all

Now put some files into c:\downloads and restart the Apache service. Open your web browser to http://mywebsite/files and you should now see the content of the directory:

Apache Alias Tutorial

Of course, this is not an in-depth tutorial on Apache’s Alias directive so here are a few useful links:

Feel free to ask any questions through the comments!

5 responses so far ↓

avatar

1. Response by : Chris on Mar 31, 2009 at 10:05 pm

Hi -
Thanks for posting this.
Would you show how this looks in a Mac environment?

For instance,
I have:
Users/me/Applications/MAMP/conf/apache/ & 8 .conf files in the apache folder.
There is one called httpd.conf but is the syntax the same?

Thanks,
chris

avatar

2. Response by : The Web Hosting Hero on Apr 1, 2009 at 7:58 am

Sorry I have no idea on how to do this on a Mac. Try it out and check the log files if it doesn’t work.

avatar

3. Response by : Mehul Ved on Jun 9, 2009 at 6:32 am

Yes, this will work for Mac, Linux, BSD’s, etc too.

avatar

4. Response by : narasimhan on Sep 12, 2010 at 1:23 am

I have a simple question and possibly very trivial.
I have not made any changes to the original config files.

my root directory is c:/xampp/htdocs (by httpd.conf)

I have afew script files (not my website) in
c:/xampp/htdocs/xampp/myscripts folder

I need to access these files in my browser.

if I type localhost/xampp/myscripts/filename.php it works
(it is too long)

but not if I type
c:/xampp/filename.php

I thought if the root directory was set to c:/xampp/htdocs apache will find all the script files below that directory.

I have tried many forums but to no purpose. I have spent a lot of time figuring out but have not found success.

last but not least, your writing is very clear I like it.

please help.

avatar

5. Response by : MiKL~ on Aug 9, 2011 at 11:35 pm

On my WAMP server, I have created a webdav directory that I called DAV. within this directory I have created an alias to another location as a sub-directory that is called ALI. On a client machine I mapped the DAV directory as a system drive. My problem is that when I use the mapped webdav drive, I cannot see the alias ALI (subdir of DAV) that I have created. However with the browser the alias appears normally. Anybody can help?

Leave a Comment




Webmaster Resources: Web Hosting Blog
© Copyright 2012 - TheWebHostingHero.com
Follow us on TwitterFacebookSubscribe to our RSS Feed