Forum Moderators: phranque

Message Too Old, No Replies

redirecting when there's no index file

         

gergoe

2:58 pm on Jun 26, 2005 (gmt 0)

10+ Year Member



Hi,

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]

...but it did not had any effect, the apache just ignored this (the rewrite log was completely empty, even on level 9).

Does anyone have an idea why it did not work, or aware of any other way to accomplish in this?

Thanks

gergoe

2:58 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



Any idea?

WiseWombat

3:42 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



I dont know if this is what your after but Try this in your apache index.

<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>

gergoe

4:07 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



No, not really; first of all I don't want to put files into the directories (anymore - I did it like that 'till now, but it is not a nice way). I need a solution which can be implemented mainly in the apache config, perhaps using an external script.

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;

skyeflye

12:17 am on Jul 1, 2005 (gmt 0)

10+ Year Member



Would it be feasible in your situation to simply create a customized "Permission Denied" (http 403 error) page that had what you wanted on it?

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.

jdMorgan

12:51 am on Jul 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you are looking for a file, try using a file-exists check:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/index\.html$ /the_page_to_show [PT,L]

You may need to add %{DOCUMENT_ROOT} to %{REQUEST_FILENAME} as well -- it seems to depend on server config.

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

gergoe

10:08 am on Jul 6, 2005 (gmt 0)

10+ Year Member



skyeflye,

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

skyeflye

5:13 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



Don't quote me on this (unless I'm right :D), but I think you would need to put Rewrite directives inside a "directory" tag in the httpd.conf file in order to get them to work.

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.