Forum Moderators: phranque
I want to make such a system, where the apache automatically displays a predefined message (webpage actually), when the requested virtual host does not have an index file in its root directory. So when I have the www.example.com host name pointing to the apache, I have it configured in a separate virtualhost, but the directory it's pointing to is empty (or at least there's no index.smthg file in it), I want to have a predefined page sent back instead of the directory list, or the http 403 error message (depending on the server/virtualhost/directory settings).
I tried to add the following to the httpd.conf
RewriteEngine On
RewriteCond h**p://%{HTTP_HOST}/ !-U
RewriteRule ^/$ the_page_to_show [NS,PT,L]
Does anyone have an idea why it did not work, or aware of any other way to accomplish in this?
Thanks
<html>
<title> * * * * * Welcome to my domain * * * * </title>
<html>
<title> * * * * * My Domain * * * * </title>
<head>
<script language="Javascript" type="text/javascript">
<!-- Hide script
//<![CDATA[
window.location.href="http://yourdomain/another-directory/another-directory/welcome.html"
//]]> End script hiding -->
</script>
</head>
<body>
</body>
</html>
The problem I want to solve is the following:
When I configure a new VirtualHost in apache, usually it is pointing to an empty directory, with no files in it. So when checking that (new) VirtualHost in a browser, you'll either get an (empty) directory listing, or a "Directory listing denied" error message - depending on the current configuration.
I'm looking for a way to show a predefined page in this case (when the root directory of a domain is empty, or at least there are no index files to be served), without copying a file to that directory.
Any idea are welcome;
The file to serve up could be specified in the main Apache conf file (and therefore aply to all virtual sites automagically), or it could be specified via separate directives inside each virtual host declaration.
Does that make sense?
Assuming you are using Apache 2.0, Here is the manual explaining the directives you would use (WARNING: APPLIES TO APACHE 2.0 ONLY!) [httpd.apache.org...]
Cheers!
-skyeflye
P.S. Remember that Internet Explorer has it's own crummy erroy pages that it serves up automatically. To make sure that YOUR custom 404 and 403 messages display instead of IE's crummy ones, you have to make the custom file that is served up at least 512 bytes or larger.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/index\.html$ /the_page_to_show [PT,L]
Make the rule and its conditions as specific as possible, to avoid doing the file-exists check and wasting CPU if it is not really necessary.
Jim
That was my second idea after using mod_rewrite, but I did not wanted to do it because if the directory browsing is enabled (Option Indexes) for a VirtualHost, then this system is not working anymore. Although this might be the only reasonable solution.
jdMorgan,
The first problem is that if I put the rules just in the middle of the httpd.conf (outside of any container), they won't take effect, even the rewritelog stays empty. What can be the problem?
The reason I used URL check in the rules I posted above is that I can't take care of all the files listed in the DirectoryIndex in one RewriteRule (would need to make 7 rules), and also that if a VirtualHost is redirected in the server config or there are rewriting rules defined, the file check would not have the desired effect.
Probably I'll make it working as it was suggested by skyeflye, esp. because we are two now who thinks this is the only (aceptable) way.
Thanks for the replies guys
Here, I apply the jdMorgan's Rewrite directives to the root directory of the "default" website. The syntax is a little hard to follow, because the first "<Directory />" is not a self-closing XML tag. It is basically saying "In the directory referred to as 'slash'", which is obviously the document root.
<Directory />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^/index\.html$ /the_page_to_show [PT,L]
</Directory>
And I *think* that setting any directives like this (in a <Directory /> tag) will apply whatever you set in there as the *default* settings for any other virtual sites on the server...unless the settings are overridden (if allowed) in the virtual site definition.
Someone please correct me if I am totally wrong on any of this.