สร้าง URL Friendly ให้กับเว็บเพื่องาน SEO กัน
947
การทำ URL Friendly นั่นจะเป็นงานที่สำคัญมากสำหรับการทำเว็บไซต์ เพราะจะเป็นวิธีการหนึ่งที่ทำให้เว็บเรานั่นสามารถค้นหาได้ง่ายและเข้าถึงง่ายขึ้น
ตัวอย่าง
- แบบเดิม /web/id/1
- แบบใหม่ /web/title/hello-world
ประโยชน์
- url ดูสวย
- ช่วยให้ url เข้าใจง่ายขึ้นมากกว่าการส่ง parameter ที่เป็นตัวเลข
- ช่วยการทำ SEO
แต่ก่อนจะทำ url ให้ดูสวยเราจะต้องจัดการกับประโยคที่มีวรรคให้เรียบร้อยก่อน ไม่อย่างนั้นจะทำให้ url ดูไม่สวย ไม่เป็นระเบียบ และเป็นการทำให้ url มีอักขระพิเศษเกิดขึ้น
ตัวอย่าง
- /web/title/hello world จะเป็น /web/title/hello%20world
วิธีการแก้ปัญหาเราจะต้องสร้าง Function นี้ เพื่อเอาไปใช้กับงานพัฒนาเว็บให้ดูสวย ดูดี มีสไตล์
PHP
<?php
function urlFriendly($wordinng)
{
$string = strip_tags($wordinng);
$string = preg_replace("`\[.*\]`U", "", $string);
$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i', '-', $string);
$string = str_replace('%', '-percent', $string);
$string = htmlentities($string, ENT_COMPAT, 'utf-8');
$string = preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i", "\1", $string);
$string = preg_replace(array("`[^a-z0-9ก-๙เ-า]`i", "`[-]+`"), "-", $string);
$result = strtolower(trim($string, '-'));
return $result;
}
echo urlFriendly("เทคนิคการ Forwarding อีเมลอัตโนมัติของ Gmail");
function urlFriendly($wordinng)
{
$string = strip_tags($wordinng);
$string = preg_replace("`\[.*\]`U", "", $string);
$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i', '-', $string);
$string = str_replace('%', '-percent', $string);
$string = htmlentities($string, ENT_COMPAT, 'utf-8');
$string = preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i", "\1", $string);
$string = preg_replace(array("`[^a-z0-9ก-๙เ-า]`i", "`[-]+`"), "-", $string);
$result = strtolower(trim($string, '-'));
return $result;
}
echo urlFriendly("เทคนิคการ Forwarding อีเมลอัตโนมัติของ Gmail");
Python
import re
def urlify(s):
# Remove all non-word characters (everything except numbers and letters)
# s = re.sub(r"[^\w\s]", '', s)
s = re.sub(r'\*(.*?)\*', '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"\s+", '-', s)
return s
def urlify(s):
# Remove all non-word characters (everything except numbers and letters)
# s = re.sub(r"[^\w\s]", '', s)
s = re.sub(r'\*(.*?)\*', '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"\s+", '-', s)
return s
Javascript
function urlFriendly(value) {
return value == undefined ? '' : value.replace(/[\s+]+/gi, '-').replace(/^-|-$/g, '').toLowerCase();
}
console.log(urlFriendly("เทคนิคการ Forwarding อีเมลอัตโนมัติของ Gmail"));
console.log(urlFriendly("Siwakorn Banluesapy"));
return value == undefined ? '' : value.replace(/[\s+]+/gi, '-').replace(/^-|-$/g, '').toLowerCase();
}
console.log(urlFriendly("เทคนิคการ Forwarding อีเมลอัตโนมัติของ Gmail"));
console.log(urlFriendly("Siwakorn Banluesapy"));
สร้าง URL Friendly ให้กับเว็บเพื่องาน SEO กัน
URL Friendly
URL Friendly Python
URL Friendly PHP
URL Friendly Javascript