Downloads often live on a separate disk or in a directory shared with a publishing job. On Ubuntu or Debian with the packaged Apache service, an Alias can serve that directory at a URL such as /downloads/ while the rest of the site still uses its DocumentRoot.

The alias maps a URL prefix to a filesystem path. A matching <Directory> block grants HTTP access to the destination, and Apache's worker account needs permission to traverse the parent directories and read the files.

A public alias makes files reachable by name even with directory listings disabled. A dedicated directory of public static files keeps private material out of the mapping.

Steps to serve a directory with Apache Alias:

Start with an enabled virtual host for an existing static site. Replace downloads.example.net with your site's hostname and use its HTTP or HTTPS scheme and port. The example uses the site file downloads.conf, the directory /srv/site-downloads outside DocumentRoot, the unused URL prefix /downloads/, and an existing public file named release.txt. Use your own values throughout and keep filesystem paths quoted. Choose a public file whose name has no matching URL at the site root. For the denied-access check, use an existing restricted static URL with a known denial status, such as /private/notes.txt.

  1. Check that Apache loads mod_alias.
    $ sudo apache2ctl -M | grep -F alias_module
     alias_module (shared)

    If no line appears, enable alias before editing the virtual host.

  2. Find the enabled virtual host for your site's hostname and port.
    $ sudo apache2ctl -S

    The matching entry lists the configuration file and line number. With a2ensite, sites-enabled/downloads.conf links to the editable file under sites-available. The User line shows which account to use in place of www-data for the file check.

  3. Check that the Apache worker account can read your public file.
    $ sudo -u www-data wc -c '/srv/site-downloads/release.txt'
    38 /srv/site-downloads/release.txt

    A byte count means the worker could open the file. If wc reports an error, check the path and the worker's search permission on the parent directories and read permission on the file before editing.

  4. Open the editable virtual host file.

    The destination must contain only files meant to be public and must be free of symlinks. If an Apache access rule on the destination or a parent directory must keep denying access to those files, stop before adding the alias.

    $ sudoedit '/etc/apache2/sites-available/downloads.conf'
  5. Insert the alias and its access rule just above the matching </VirtualHost> line.

    Require all granted allows any client that reaches the site to request files in the destination directory. If a redirect, proxy, alias, or access rule already handles the prefix, resolve that conflict before adding the mapping.

        Alias "/downloads/" "/srv/site-downloads/"
     
        <Directory "/srv/site-downloads">
            Options None
            AllowOverride None
            Require all granted
        </Directory>

    With a trailing slash on both Alias arguments, Apache maps /downloads/ and paths beneath it. The <Directory> path names the same filesystem directory. Options None disables generated listings, and AllowOverride None disables per-directory .htaccess overrides in the standard site configuration.

  6. Save the file.
  7. Close the editor.

    sudoedit writes the saved changes to the virtual host file when the editor closes.

  8. Test the Apache configuration before reloading.
    $ sudo apache2ctl configtest
    Syntax OK

    If the test reports an error, correct the file and rerun the test before reloading.

  9. Reload the running Apache service after the syntax test passes.

    A reload activates all pending Apache configuration changes. If the new mapping needs to be rolled back, remove the inserted lines, test the configuration, and reload again.

    $ sudo service apache2 reload
  10. Confirm that the same filename returns 404 Not Found at the site root.
    $ curl -sS -I http://downloads.example.net/release.txt
    HTTP/1.1 404 Not Found
    ##### snipped #####
  11. Recheck an existing restricted URL without credentials if your site has one.
    $ curl -sS -I http://downloads.example.net/private/notes.txt
    HTTP/1.1 403 Forbidden
    ##### snipped #####

    The response should match the denial status the URL returned before the change.

  12. Verify that the file returns 200 OK under the alias URL.
    $ curl -sS -I http://downloads.example.net/downloads/release.txt
    HTTP/1.1 200 OK
    ##### snipped #####
    Content-Type: text/plain

    -I requests response headers. A 403 Forbidden response can point to a directory access rule or insufficient filesystem permissions. A 404 Not Found response can point to the wrong virtual host, URL prefix, or filename.

Share this guide