ASP.NET (C#, VB.NET)

Search engine friendly URL ด้วย ASP.NET กับ C#

Search engine friendly URLs using ASP.NET (C#.NET)มาทำ Search engine friendly URLs ด้วย .NET เทคโนโลยีทุกวันนี้ในหลายองค์กรและหลายบริษัทและหลายเว็บไซต์ที่จัดทำเพื่อเผยแพร่ข่าวสารหรือข้อมูลนั้น Information หรือ สารสนเทศทางด้าน IT จากที่เป็น Static Page HTML, XHTML ก็มาสนใจในรูปแบบ Dynamic Content หรือการ Query ข้อมูลจาก Database ที่ใช้กันซึ่งเวลาดึงข้อมูลก็จะ แสดงผลบน File เดียวแต่ต้องระบุ Parameter ที่เป็นตัวเชื่อม Primary Key ของ ตารางข้อมูลนั้นๆดังเช่น

http://www.mydomain.com/?pageid=4

นั่นคือ Content จะแสดงผลผ่านหน้าเว็บ .aspx โดยดึงชุดเนื้อหา จากตาราง เนื้อหาที่มี Field ของ Primary Key คือ pageid =13 ครับ ซึ่งทาง LINUX นั้นมีการจัดการ .htaccess ไฟล์ให้ Support ฟังก์ชั่น mod_rewrite ของ LINUX เพื่อแปลง parameter ให้หายไปและทำ URLs ให้สอดคล้องกับ เนื้อหาเพื่อให้เหมาะกับ Search Engine Optimizered เช่นลบเครื่องหมายจำพวก ?, &, %, +, =, $, cgi-bin, .cgi. Hence เป็นต้น

แล้วทำถ้า Hosting Server เป็น Windows ล่ะ การ Develope ต้องใช้ ASP.NET ทำ mod_rewrite ก็ไม่ได้ จะต้อง
ทำยังไงดี ก็เลยหาวิธีมาแก้ให้เป็น URLs Friendly for SEO อย่างน้อยๆ ก็ยังมี link ที่เข้าท่าอยู่

มาเริ่มกันครับเป้าหมายของเราคือ

http://www.mydomain.com/?pageid=4

เปลี่ยนเป็น

http://www.mydomain.com/page4.aspx

ดูๆแล้ว น่าจะเป็นอะไรที่เข้าใจง่าย อย่างแรกเลยครับสร้างไฟล์ Global.asax แก้ไขส่วนของ event

Application_BeginRequest

ครับโดยแก้ไขดัง code ด้านล่าง

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext incoming = HttpContext.Current;
    string oldpath = incoming.Request.Path.ToLower();
    string pageid; // page id requested

    // Regular expressions to grab the page id from the pageX.aspx
    Regex regex = new Regex(@"page(d+).aspx",  RegexOptions.IgnoreCase |
        RegexOptions.IgnorePatternWhitespace);
    MatchCollection matches = regex.Matches(oldpath);

    if(matches.Count > 0)
    {
        // Extract the page id and send it to Process.aspx
        pageid = matches[0].Groups[1].ToString();        
        incoming.RewritePath("Process.aspx?pageid=" + pageid);
    }
    else
        // Display path if it doesn’t containt pageX.aspx
        incoming.RewritePath(oldpath        
    }
}

จาก code ด้านบนนั้น URLs ที่มี parameter จะถูกลดรูปเหลือ pageX.aspx (X แทนที่ pageid 1,2,3,4,…) ครับ
ลองเทสดูก็ได้ครับ

<%
    string pageid = Request.QueryString["pageid"];
    // Create the page content based on this pageid taken here
%>

น่าจะพอใช้งานได้จริงนะครับห

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Leave a Reply

Back to top button
Game & Mobile Development AR VR XR
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน