Place a static file in root directory in Apache Server

Sometimes you are required to place files like sitemaps or other files in the root location.

Specially while using Web frameworks like Django or anything else, It can be bit tricky to do so within the framework.. So this method will flawlessly irrespective of which framework you are using.

If you are running Apache server, it’s quite easy with an Alias configuration

1. Just open up your Apache Config file and add the following line at the
bottom of that file.

In ubuntu 16.04 you can find the config file here: /etc/apache2/apache2.conf

Alias /example.txt /path/to/example.txt

2. Now restart your apache server.

sudo service apache2 restart

After this you can access the file kept at /path/to/example.txt as /example.txt

Cool!!

But what If you have multiple sites on VirtualHost and would like to have the Alias specific to that site?

Goto your VirtualHost and the Alias at the beginning of the VirtualHost

<VirtualHost *:80>

    Alias /example.txt /path/to/example.txt
....
....

and now the /example.txt will only work on that particular VirtualHost only.

Note: If you have SSL virtual host also then do the same for the SSL virtual host file other wise you will get a 404 error.

Quite easy!

Reference: https://httpd.apache.org/docs/2.4/mod/mod_alias.html#alias

Leave a Reply

Your email address will not be published. Required fields are marked *