Example:
/brand/discountinued-model.htm -> /brand/
Does anyone have a favorite Perl script for doing this? Would it be possible with a clever Redirect line in my .htaccess (don't have mod_rewrite installed at my host)?
#!/usr/local/bin/perl -w
use strict;
print "Status: 301\n";
if ( $ENV{'REQUEST_URI'} =~ /(.*)\/[^\/]+$/ ) {
print "Location: $1\n\n";
} else {
print "Location: /\n\n";
}
Let me comment on that for you a bit:
#!/usr/local/bin/perl -w
use strict;
# that stuff should go at the top of every perl script.
print "Status: 301\n"; # change the 404 status to a redirect (302 if you like)
if ( $ENV{'REQUEST_URI'} =~ /(.*)\/[^\/]+$/ ) {
# match everything up to the last "/" in the requested URL.
print "Location: $1\n\n";
# send the redirect to one level up.
} else {
# if the match thing above failed, just send them to the root.
# you could return a 404 here or send them to a real page or something.
print "Location: /\n\n";
}
make this script something like "/cgi-bin/htredirect.cgi" and then make a .htaccess that looks like this:
ErrorDocument 404 /cgi-bin/htredirect.cgi