I am running Linux Mint 21.3 Cinnamon, and installed XAMPP 7.4.33 because I want to test a web application using a particular version of PHP. XAMPP installs Apache of course, and the first thing I want to do is to get the Alias
directive working.
XAMPP installs to /opt/lampp/
and inside there is a directory htdocs
and inside here, I created a directory called alias-test
, and then 2 subdirctories called old
and new
.
So the structure is like this...
/opt/
lampp/
htdocs/
alias-test/
old/
new/
test.html
At this point, with Apache running, I can browse to http://localhost/alias-test/old/ and to http://localhost/alias-test/new/ and as expected, in both cases, I get an Apache-generated listing of the files in these directories – that is, the "old" directory shows as empty, and I can see the "test.html" file inside the "new" directory.
Now, what I want to achieve is to be able to navigate to http://localhost/alias-test/old/test.html and for Apache to load the file /opt/lampp/htdocs/alias-test/new/test.html
. So in /opt/lampp/htdocs/alias-test/
I created a .htaccess
file and put this inside...
Alias /alias-test/old /opt/lampp/htdocs/alias-test/new
Now if I navigate to http://localhost/alias-test/old/test.html I get an Apache error:
Server error!
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
If you think this is a server error, please contact the webmaster.
Error 500
localhost
Apache/2.4.54 (Unix) OpenSSL/1.1.1s PHP/7.4.33 mod_perl/2.0.12 Perl/v5.34.1
So, what's going on? What have I overlooked to get this minimal viable example working?
As a server administrator when you're confronted with server errors the first place would be to look at logs on the server. Apache and other web servers will generally log the reason why they responded with a particular error.
In this case the reason for the error can be found in the context section of the
Alias
directive,Since
.htaccess
is not listed in the context section, theAlias
directive is simply not allowed in a.htaccess
file.Add the
Alias
in your main httpd.conf and/or the VirtualHost configuration snippet that gets included in your httpd.conf and it will work as expected.Despite what many people continue to parrot: you do not need to use .htaccess files.
They are a last resort, never the first choice when you have admin/root rights, because they come with performance penalties .
The recommendation from the Apache project is: