วิธี redirect เว็บไซต์ให้ไปหน้าอื่น

ภาษาอังกฤษ สอบเข้า ม.1 | คณิตศาสตร์ สอบเข้า ม.1 | วิทยาศาสตร์ สอบเข้า ม.1 | สอบ TGAT | AP | O-NET คืออะไร |

วิธี redirect เว็บไซต์ให้ไปหน้าอื่น

3
ม.ค.
    2019-01-03 20:30:34   
    1230
      webkroox@gmail.com

ปกติแล้วใน 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="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>";}
}?>

บทความล่าสุด

17
ก.ค.
วิธีการทำให้ YouTube เล่นอัตโนมัติ
วิธีการทำให้ You Tube เล่นอัตโนมัติ   วิธีการทำให้ You Tube เล่นอัตโนมัติ ..
                                                                                                                                                                  
28
ต.ค.
วิธีแก้ดู คลิปวีดีโอ Facebook เป็นเส้นๆ
วิธีแก้ดู คลิปวีดีโอ Facebook เป็นเส้นๆ ลองปิดตัวเร่งกราฟฟิกส์ https://pureinfotech.com/disable-har..
                                                                                                                                                                  
14
ต.ค.
คู่มือการสมัคร และติดตั้ง Google Analytics
1. ไปยังเว็บไซต์ http://www.google.com/analytics เพื่อสมัครใช้งาน Google Analyti..
                                                                                                                                                                  
9
ต.ค.
วิธีนำ Google Map มาติดตั้งบนบนเว็บไซต์
มาเริ่มขั้นตอนการติดตั้ง Google Map บนบนเว็บไซต์ไปพร้อมๆกันเลย โดยในที่นี้จะเริ่มด้วยการใช้ Google M..
                                                                                                                                                                  
7
ต.ค.
การแปลง 3D และแอนิเมชั่น ภาพหมุน Logo หมุนรอบตัวเอง
@-webkit-keyframes spinner { from { -webkit-transform: rotateY(0deg); } to { -w..