#!/usr/bin/perl -w use strict; $|++; my $URL = "http://www.geekcruises.com"; my $DIR = "/data/web/geekcruises"; my $USER = 2100; my $GROUP = 2100; my $MODE = 0644; use Cwd qw(abs_path); use File::Finder; my @dirs = File::Finder->type('d')->in($DIR); # print "$_\n" for @dirs; for my $dir (@dirs) { my @symlinks = grep -l, glob "$dir/*"; # print "$dir: @symlinks\n"; my @deletes; my $htaccess = "$dir/.htaccess"; for my $symlink (@symlinks) { defined(my $path = readlink($symlink)) or warn("Cannot read $symlink: $!"), next; $path =~ m{^/} and warn("skipping absolute $path for $symlink\n"), next; my $abs_path = abs_path("$dir/$path"); # print "$symlink -> $path => $abs_path\n"; $symlink =~ m{^\Q$DIR\E/(.*)}s or warn("$symlink doesn't begin with $DIR"), next; my $original_url = "/$1"; $abs_path =~ m{^\Q$DIR\E/(.*)}s or warn("$abs_path doesn't begin with $DIR"), next; my $redirect_url = "$URL/$1"; unless (@deletes) { ## print "in $dir...\n"; open NEW, ">$htaccess.NEW" or die; if (open OLD, $htaccess) { print NEW ; } } print NEW "Redirect $original_url $redirect_url\n"; push @deletes, $symlink; } if (@deletes) { close NEW; chown $USER, $GROUP, "$htaccess.NEW" or die "Cannot chown $htaccess.NEW: $!"; chmod $MODE, "$htaccess.NEW" or die "Cannot chmod $htaccess.NEW: $!"; ! -e $htaccess or rename $htaccess, "$htaccess.OLD" or die "Cannot mv $htaccess $htaccess.OLD: $!"; rename "$htaccess.NEW", $htaccess or die "Cannot mv $htaccess.NEW $htaccess: $!"; for (@deletes) { unlink $_ or warn "Cannot unlink $_: $!"; } } }