原文:http://bash.cyberciti.biz/file-management/send-http-code-301-moved-permanently-redirection/
以下是该shell脚本的源代码:
#!/bin/bash
# Shell script to update old php files to point out to new CMS url
# it placed http code 301.
# You must create an array in following format:
# path/to/old/url1.php path/to/new/url/slug/
# --------------------------------------------------------------------------
# Copyright (C) 2008 nixCraft project # This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
declare -a t=("http://domain.com/1.php" "http://domain.com/new/path/slug/"
"http://domain.com/2.php" "http://domain.com/new/path/slug/"
"http://domain.com/3.php" "http://domain.com/new/path/slug/"
"http://domain.com/4.php" "http://domain.com/new/path/slug/"
"http://domain.com/5.php" "http://domain.com/new/path/slug/")
# set array total array size -1 ; if you have 5 item it should set to 4
for i in `seq 0 4`
do
#only work on zero or even element
if [ $i -eq 0 -o $( expr $i % 2 ) -eq 0 ]; then
s="${t[$i]}"
d="${t[` expr $i + 1 ` ]}"
file=$(basename $s)
echo "Patching $file...."
if [ -f $file ]; then
# if file exists, create new php redirection code
echo '<?php'>$file
echo 'header("HTTP/1.1 301 Moved Permanently");'>>$file
echo 'header("Location: ' $d '");'>>$file
echo '?>'>>$file
fi
fi
done |