In the directory /var/www I have the following directories: foo, bar and baz. I want foo to be my document root, that is, when www.example.com/ is requested, the directory /var/www/foo is served. Otherwise, for all other directories, /var/www/ is considered to be document root, so www.example.com/bar/ serves /var/www/bar/ and www.example.com/baz/ shows /var/www/baz.
I could set the document root to /var/www/foo/ and move the directories bar and baz there, which I don't want to do. The server has a main purpose and a few other, unrelated tasks which are contained in these additional directories. I don't want to mix this.
I can set the document root to /var/www/foo and use the Alias directive to map the other directories to the required locations, so in the case above
Alias /bar /var/www/bar
Alias /baz /var/www/baz
However, each time that I add another directory, I would have to modify the apache configuration.
Is there a better way?
Related
I have a Google Compute Engine instance(installed apache, php, mysql etc.) and a custom domain name. I can do add my Custom Domain to my Google Compute Engine.
Lets my domain name be www.try.com .
my instance's "www" folder;
--->try
--->somethingelse2
--->somethingelse3
..
How can I set "www.try.com" access to only "try" folder? Thanks.
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. Therefore, the files and directories underneath the DocumentRoot make up the basic document tree which will be visible from the web.
If a directory is requested (i.e. a path ending with /), the file served from that directory is defined by the DirectoryIndex directive. For example, if DocumentRoot were set as above, and you were to set:
DirectoryIndex index.html index.php
You only need to modify the path in the Directory index.
You can find more information here
Background
Using Ubuntu with apache2:
In my sites-enabled config I have an Alias command like so:
Alias "/someDir" "/webSiteDir"
It works great.
So, the absolute path of /someDir is actually a "virtual directory" which effectively exists at /var/www/html/someDir. Whereas /webSiteDir actually exists at the root of the Ubuntu box's file system.
Question 1
How would I créate an Alias from one directory in the document root to another directory also in the document root? The manual for Apache specifically says that Alias should be used for redirecting to directories outside the document root. So what should I use if both the "virtual directory" and the actual directory are inside the document root?
Right now, I have it working with the following line:
Alias "/anotherDir" "/var/www/html/thisDir"
That seems quite clunky when the document root is explicitly defined just a few lines up as var/www/html within the very same config.
Should I be using a different command other than Alias for when both directories are under the same document root?
Question 2
How does the apache config even correctly determine the absolute path of different entries?
For example, in the above line:
Alias "/anotherDir" "/var/www/html/thisDir"
/anotherDir is treated as a "virtual directory" under /var/www/html/ but the second part which is /var/www/html/thisDir is treated as an absolute path with regards to the Ubuntu file system.
Ok fine, maybe that is just the syntax of the Alias command, in that the first entry is always relative and the second entry is always absolute.
Well what about the <Directory> attribute?
I have one entry:
<Directory "/">
which seems to refer to the document root of /var/www/html/ - a relative path.
And I also have:
<Directory "/thisDir">
which seems to refer to /var/www/html/thisDir - another relative path
Whereas I have another:
<Directory "/webSiteDir">
which seems to refer to an absolute path in the file system of /webSiteDir.
So, is my config file just messed up, or does apache somehow know when something is a relative path to the document root, or an absolute path from the system root?
Alias does not care much about your documentroot in a sense. That is, just define Alias inside your VirtualHost, first argument is a new virtual path and second is always a filesystem path, can be or it can not be inside the documentroot, Alias does not care or differentiate about it.
So, to question 1.
Always use alias to define virtualpaths, irrespectively where the filesystem is, just use it when you need it.
To question 2.
You have a BIG misunderstanding, Directory ALWAYS specifies a full path in your OS, that is <Directory "/">is / in your OS Filesystem and the same with the rest of your "Directory" examples.
If you wanted to affect a path created with Alias, you would use "Location" or Directory with the full path, and that one is really relative to documentroot.
I have apache setup to serve sites in /var/www, and I have many sites in this directory. Now I don't own any of these sites and cannot update any of their content.
The problem is most of these websites use absolute path for their assets like <img src="/img/foo.png">, and apache tries to load them from /var/www/ instead of say /var/www/example1/.
I tried to add a .htaccess to /var/www/ to rewrite every paths to assets to a subdirectory, but I can't find a way to make assets from /var/www/example1/ loaded from this directory and assets from var/www/example2/ loaded from example2 instead of example1.
Is there a way to make this work? I don't really like the .htaccess solution in /var/www/, at first I wanted to use a vHost configuration for every sites but I didn't find how to do it (at least without having a domain name for each site).
Thanks!
How would I indicate Alias directives on shared hosting.
The file I want to serve is located:
home/user/public_html/the_file_I_want_to_serve.html
I would like to serve the file described above in:
home/user/desired_folder/subfolder1/subfolder2/HERE
I read the Apache's documentation on Alias directives, but I am unsure if I place directives in public_html/.htaccess or in desired_folder/.htaccess
Then I wonder, would directives in htaccess recognize a folder outside of its domain?
the url to load home/user/desired_folder/subfolder1/subfolder2/HERE is a subdomain like temp.domain.com which points to home/user/desired_folder/subfolder1/subfolder2
The Alias directive is not allowed outside of the server/vhost config. Since the htaccess file is a "per directory" context, there's no way to do anything outside of the context of the document root. Which means you can't do/know anything outside of the root (which I'm assuming is /home/user/public_html/. You'd have to move the "desired_folder" into the public_html directory, or at least symlink it from the public_html directory.
Here is the folder layout.
/outside is not normally accessible anywhere on example.com
/public_html is the folder for http://example.com
How can I use Apache's htaccess to make the url http://example.com/outside/ to show content from the folder /outside?
Thanks!
How can I use Apache's htaccess to make the url http://example.com/outside/ to show content from the folder /outside?
You can't. As far as I have found out, Apache prevents directives in .htaccess files linking to locations outside the current web root.
You would have to do this in the central configuration:
Alias /outside /path/to/your/outside
You may be luckier with a symlink if you can turn FollowSymLinks on.