การทำ redirect เพื่อให้กระโดดไปหน้าเว็บอื่น

inplug profile image inplug
การทำ URL Redirect

ปกติแล้วใน Web Server จะมีไฟล์ที่เป็นไฟล์แรกสำหรับการแสดงเพจหน้าแรกซึ่งจะวางอยูในตำแหน่ง Document Root แต่ในบางครั้งเมื่อผู้ใช้มีการเรียกเข้ามาที่ URL หรือ ไฟล์ดังกล่าวบน Web Server แล้ว อาจจะมีความจำเป็นที่ URL ดังกล่าว ยังไม่พร้อมที่จะให้บริการ เราก็สามารถจะเขียน Code ในไฟล์ดังกล่าว ให้มีการเปลี่ยนเส้นทางไปเรียกไฟล์อื่น ซึ่งอาจจะอยู่ในอีกไดเร็คทอรี่หรืออีกโฟลเดอร์บน Web Server ตัวเดียวกันหรืออาจจะเปลี่ยนเส้นทางไปเป็น Web Server อีกตัวก็ได้

ตัวอย่างการทำ redirect แบบง่าย

การ Redirect แบบง่ายสามารถทำได้ด้วยการใช้ meta tag ของ HTML Code ดังนี้

<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=http://www.yourdomainname.com">
</head>
</html>

ซึ่งถ้าเอา Source Code ข้างบนนี้ไปเป็นไฟล์หลักในตำแหน่ง Document Root บน Web Server ก็จะทำให้เว็บเพจถูก Redirect ไปยัง http://www.yourdomainname.com โดยทันที เพราะค่าของ CONTENT=0 แต่ถ้าต้องการหน่วงเวลาให้ผู้ใช้ได้อ่านข้อความบางอย่าง ก่อนการ Redirect ก็สามารถทำได้ด้วยการกำหนดค่า CONTENT ไม่เป็น 0 ดังนี้

<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="5;URL=http://www.yourdomainname.com">
</head>
<body>
<center>
คุณกำลังเข้าสู่เว็บเพจใหม่
<a href="/RedirectExternalUrl?url=http://www.yourdomainname.com">www.yourdomainname.com</a>
</center>
</body>
</html>


แต่วิธีการ Redirect แบบข้างบนนี้จะทำให้ชื่อ URL ตรงช่อง Address ของ Browser ถูกเปลี่ยนไปเป็นชื่อใหม่ คือ http://www.yourdomainname.com ซึ่งหากไม่ต้องการให้ URL เปลี่ยนเป็นอันใหม่จะต้องใช้คำสั่ง frame (อาจจะเรียกว่า frame redirect หรือ frame to URL)

<html>
<head>
<title>redirect page</title>
</head>
<frameset cols="*">
<frame src="http://www.yourdomain/yourname">
</frameset>
</html>

ตัวอย่างการทำ URL Redirect สำหรับ Web hosting

<html>
<head>
<script langquage='javascript'>
var url=document.location; //ตรวจสอบว่าผู้ใช้ต้องการเรียกเข้าสู่โดเมนไหน
if ((url=="http://www.firstdomainname.com") || (url=="http://www.firstdomainname.com/"))
{
window.location="http://www.yourdomainname.com/firstdomainname";
}
else if ((url=="http://www.seconddomainname.com") || (url=="http://www.seconddomainname.com/"))
{
window.location="http://www.yourdomainname.com/seconddomainname";
}
else
{
window.location="http://www.yourdomainname.com/underconstruction.html";
}
</script>
</head>
</html>


หากต้องการไม่ให้ URL ในช่อง Address ของ Browser เปลี่ยนไปเป็นตำแหน่งที่แท้จริง ทำได้ด้วยการใช้คำสั่ง frame เข้ามาช่วยดังนี้

<html>
<script langquage='javascript'>
url=document.location;
if ((url=="http://www.firstdomainname.com") || (url=="http://www.firstdomainname.com/"))
{
document.write("<head><title>www.firstdomainname.com</title></head>");
document.write('<frameset cols="*">');
document.write('<frame src="http://www.yourdomainname.com/firstdomainname">');
document.write("</frameset>");
}
else if (url=="http://www.seconddomainname.com") || (url=="http://www.seconddomainname.com/"))
{
document.write("<head><title>www.seconddomainname.com</title></head>");
document.write('<frameset cols="*">');
document.write('<frame src="http://www.yourdomainname.com/seconddomainname">');
document.write("</frameset>");
}
else
{
document.write('<frameset cols="*">');
document.write('<frame src="http://www.yourdomainname.com/underconstruction.html">');
document.write("</frameset>");
}
</script>
</html>


การทำ URL Redirect โดยใช้ PHP

หากมีความจำ เป็นที่จะต้องทำการ Redirect โดยการใข้ภาษา PHP ก็สามารถทำได้ โดยใช้รูปแบบโครงสร้างของภาษาเป็นแบบเดียวกันกับที่กล่าวมาเพียงแต่เปลี่ยน คำสั่งดังนี้
ซึ่งเป็นแบบที่ address ไม่เปลี่ยนแปลง (frame to URL)

<?
switch ($SERVER_NAME){
case "www.firstdomainname.com";{
echo '<frameset cols="*">';
echo '<frame src="http://www.yourdomainname.com/firstdomainname">';
echo "</frameset>";
break;}
case "www.seconddomainname.com";{
echo '<frameset cols="*">';
echo '<frame src="/www.yourdomainname.com/seconddomainname">';
echo "</frameset>";
break;}
default;{
echo '<frameset cols="*">';
echo '<frame src="http://www.yourdomainname.com/underconstruction.html">';
echo "</frameset>";}
}?>

 

.......................................ที่มา=http://www.thaitumweb.com................................
ความคิดเห็น

ประกาศล่าสุดในบอร์ดเดียวกัน

pim11 Icon หาผู้ลงทุนกับทางบริษัท Spopback อ่าน 78 3 เดือนที่ผ่านมา
3 เดือนที่ผ่านมา
5 เดือนที่ผ่านมา
9 เดือนที่ผ่านมา
tinyofsky Icon nft art คือ อ่าน 368 9 เดือนที่ผ่านมา
9 เดือนที่ผ่านมา
2 ปีที่ผ่านมา
2 ปีที่ผ่านมา
3 ปีที่ผ่านมา
3 ปีที่ผ่านมา
3 ปีที่ผ่านมา
3 ปีที่ผ่านมา