Redirect if a website root is empty?
Posted by Steve on Thu 6 Mar 2008 at 00:15
This should be a simple problem to solve, but I've yet to find a good solution, so any assistance would be most welcome. If you'd like to redirect to another website if a directory root has no files in it, how would you do so?
Assume that you have an Apache virtual host which has a directory root with no files within it. In that case it would be nice to redirect to a new location with a message such as "there are no files here yet".
The naive approach would be to have something like this:
# Excluding existing files and folders
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
# and if there is nothing in the root redirect
RewriteRule ^/$ http://www.steve.org.uk/
Unfortunately this does not work, and I'm out of ideas..
RewriteEngine on
<Directory /var/ww>
# this is the key.
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ http://www.steve.org.u/
</Directory>
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
That extra line makes all the difference. Thanks very much!
[ Parent | Reply to this comment ]
http://httpd.apache.org/docs/1.3/misc/rewriteguide.html
Topic : Redirect Failing URLs To Other Webserver
[ Parent | Reply to this comment ]
I can't recall how to do that, but a google search will find it for you.
Cheers!
[ Parent | Reply to this comment ]
But, if you want to show user a page from other server through you own...
That's a bit different task.
I'm not sure if mod_rewrite can help, maybe mod_proxy would be more appropriate.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
Using the ReWrite engine seems a bit overly complex for a simple redirect like this.
--
"It's Not Magic, It's Work"
Adam
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
The intention is that this default page will be present for resellors, etc, and that when content is uploaded it will be seamlessly removed.
So the less that I have to do, and maintain,the better!
[ Parent | Reply to this comment ]
(Now, arguably, if you're not competent enough to work out how to fix that yourself, you shouldn't be running a webserver... but that's an argument for another day!)
[ Parent | Reply to this comment ]
So without having a page in the document root you want an automatic "redirect" to a standard holding page, then when any content arrives, the automatic redirect deactivates without you or the end user doing anything.
Given the almost almost zero knowledge of the end user this isn't going to be that easy without using the Rewrite rule. The error document while more elegant, requires more administration from your side, so not what you want.
--
"It's Not Magic, It's Work"
Adam
[ Parent | Reply to this comment ]
- disable directory indexes
- setup global error documents
Alias /error/ /srv/www/errorr/
- add an error handler for missing indexes
<LocationMatch "^/+$">
Options -Indexes
ErrorDocument 403 /error/noindex.html
</LocationMatch>
Benjamin
http://benjamin-schweizer.de/?debian
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
<?php
header("Location: http://www.domainname.com/";); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
[ Parent | Reply to this comment ]
Thanks.
[ Parent | Reply to this comment ]