Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache - Mapping url to local path for static content

Tags:

url

static

apache

My httpd.conf

<VirtualHost *:80>
    ...
    DocumentRoot /home/www/static
    ...
    <Directory /home/www/static>
    Order Allow,Deny
    Allow from all
    </Directory>

    <Location "/foo">
    SetHandler None
    </Location>
</virtualhost>

I have a file at /home/www/static/foo/helloworld.txt. And if I go to http://localhost/foo/helloworld.txt I will see that file.

Now, for some irrelevant reason, I want to change the urls. The above url should return nothing, while http://localhost/bar/helloworld.txt should return the file. And I want to achieve this, without changing anything in the directory structure.

How is that done?

like image 931
Erik Ninn-Hansen Avatar asked Jul 18 '11 17:07

Erik Ninn-Hansen


People also ask

What is DocumentRoot in Apache server?

The DocumentRoot is the top-level directory in the document tree visible from the web and this directive sets the directory in the configuration from which Apache2 or HTTPD looks for and serves web files from the requested URL to the document root. For example: DocumentRoot "/var/www/html"

How does Apache know which html file to serve when you enter the URL?

In deciding what file to serve for a given request, httpd's default behavior is to take the URL-Path for the request (the part of the URL following the hostname and port) and add it to the end of the DocumentRoot specified in your configuration files.

What is IfModule in Apache?

The <IfModule> directive is very similar, except it encloses directives that will only be applied if a particular module is available in the server. The module must either be statically compiled in the server, or it must be dynamically compiled and its LoadModule line must be earlier in the configuration file.


1 Answers

You can use Alias to map different url paths to filesystem paths:

Alias /bar /home/www/static/foo

See http://httpd.apache.org/docs/2.2/mod/mod_alias.html#alias for more info.

like image 163
evil otto Avatar answered Sep 20 '22 11:09

evil otto