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


Posted by Anonymous (82.32.xx.xx) on Thu 6 Mar 2008 at 00:22
    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 ]

Posted by Steve (80.68.xx.xx) on Thu 6 Mar 2008 at 09:46
[ Send Message | View Steve's Scratchpad | View Weblogs ]

That extra line makes all the difference. Thanks very much!

Steve

[ Parent | Reply to this comment ]

Posted by Anonymous (217.229.xx.xx) on Fri 7 Mar 2008 at 09:03
Here you find the solution of mod_rewrite :
http://httpd.apache.org/docs/1.3/misc/rewriteguide.html

Topic : Redirect Failing URLs To Other Webserver

[ Parent | Reply to this comment ]

Posted by cjaysson (75.95.xx.xx) on Thu 6 Mar 2008 at 05:17
[ Send Message ]
Wouldn't it be fairly simple to just write a redirect into the html code of the index page? Or are you looking for a more global approach, i.e. a custom 404 error, which tells people that there is nothing to see there?

I can't recall how to do that, but a google search will find it for you.

Cheers!

[ Parent | Reply to this comment ]

Posted by Anonymous (92.241.xx.xx) on Thu 6 Mar 2008 at 06:48
I'd also agree that redirect from 404 error would be more simplea nd usable.

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 ]

Posted by barbacha (82.227.xx.xx) on Thu 6 Mar 2008 at 22:50
[ Send Message ]
ErrorDocument 404 http://somewhere.else/sorry.html

That's an easy one ! :)

[ Parent | Reply to this comment ]

Posted by ajt (81.6.xx.xx) on Fri 7 Mar 2008 at 09:50
[ Send Message | View Weblogs ]
I think your solution is the best as long as you remember to update the redirect once there is content.

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 ]

Posted by Steve (80.68.xx.xx) on Fri 7 Mar 2008 at 10:00
[ 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!

Steve

[ Parent | Reply to this comment ]

Posted by Anonymous (137.222.xx.xx) on Fri 7 Mar 2008 at 10:42
Absolutely! It would be great if the debian packages could use this instead of their current implementation of apache2-default (see /etc/apache2/sites-available/default in etch, for example). We get an amazing number of questions on #debian about how to enable a website because people can't disable the "it's working" page.

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

Posted by ajt (81.6.xx.xx) on Sun 9 Mar 2008 at 17:21
[ Send Message | View Weblogs ]
Ah, that's subtly different from what you originally posted. What you want is for the server to do something until the user loads their first page. Then their page gets served without you having to do anything.

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 ]

Posted by Anonymous (87.139.xx.xx) on Thu 6 Mar 2008 at 08:26
Here's what Red Hat did in the past:
- 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 ]

Posted by Anonymous (83.90.xx.xx) on Fri 7 Mar 2008 at 21:08
Simplest and most efficient way is to use and absolute path with DirectoryIndex (although it would also apply to subdirctories).

[ Parent | Reply to this comment ]

Posted by Anonymous (125.7.xx.xx) on Wed 12 Mar 2008 at 03:34
make an index.php file and put below code in this. and change the domain name where you want to redirect.



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

Posted by Anonymous (165.21.xx.xx) on Fri 14 Mar 2008 at 23:45
I like your implementation... simple yet powerful :)

Thanks.

[ Parent | Reply to this comment ]

User Login

Username:

Password:

[ Advanced Login ]

Register Account

Quick Site Search