Shell Script to send HTTP Code 301 Moved Permanently Followed by Actual URL Redirection

This script depends upon shell array.

#!/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 <http://www.cyberciti.biz/tips/contact-us>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
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
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.