Pages

Monday, January 9, 2012

Alias a URL on Apache

Many a times I have needed to share a page using Apache, without copying the page into the '/var/www' folder. Discussed below is one of the solutions that I often use.

Lets say you have a folder '/home/user/images' that you would like to share over Apache but you might not want to put it over into '/var/www' maybe because you have other scripts performing on this folder or taking backup from time to time.

In such a scenario, Apache's mod_alias comes to our rescue. This module allows us to provide an alias URL for the required directory. To achieve the same, insert the following lines in the '/etc/apache2/httpd.conf'

Alias /image /ftp/pub/image

<Directory /ftp/pub/image>
  Order allow,deny
  Allow from all
</Directory> 
Care must be taken regarding the trailing slash ('/') in the directory's name. Among the options available, I suggest not using the trailing slash in any path (neither the alias, nor the actual path).

This is not the end to the process though. One important step is to ensure that the path is 'executable' by other users as well. Each of the parent directory should be also be executable by other users.

Use 'chmod 711' on each of the parent directories.

Voila, now you are done. Just restart apache and visit the URL
site-address/alias

2 comments:

  1. You can also use a symbolic link. I have my TV shows folder symlinked into /var/www, so I can stream videos to my phone in classes like EVS (you won't believe we have 4 consecutive hours per week of those shitty things; nice time to watch TV shows, though :P).

    Do you know of any way to serve files on an NTFS drive through Apache? Because NTFS doesn't have a well defined concept of file permissions, so it defaults to -rwx------ on being mounted, and that can't be changed with chmod :(

    ReplyDelete