mod_rewrite ใช้เพื่อแปลง URL ที่ยาวและซับซ้อนให้ดูง่ายขึ้น ซึ่งมีประโยชน์ต่อ search engine ด้วย เพราะ search engine ไม่ค่อยชอบ url ที่มี query string ยาวๆ
การใช้งาน mod_rewrite ทำได้โดย สร้าง .htaccess file และเริ่มด้วยคำสั่ง RewriteEngine on เพื่อเปิดการทำงาน
(หากใครใช้ wordpress จะต้องใช้วิธีแก้ permalinks ซึ่งค่อนข้างง่ายและสะดวกกว่าเขียนเองพอสมควร อ่านวิธีแก้ permalinks ใน wordpress เพื่อประโยชน์ทาง SEO ได้ที่ https://support.hostneverdie.com/index.php?rp=/knowledgebase/4/-permalinks--wordpress.html )
การใช้ Mod_Rewrite ในการทำ Redirects
RewriteRule ^old\.html$ new.html
การใส่ rule แบบนี้เป็นการกำหนดว่า เมื่อมีการใส่ url เป็น old.html มันก็จะถูกส่งไปที่ไพล์ new.html
แต่ผู้เยี่ยมชมยังเห็น URL บน browser เป็น old.html อยู่เหมือนเดิม
RewriteRule จะประกอบด้วยกัน 3 ส่วน
1. Call to action RewriteRule ขึ้นต้นบรรทัด กำหนดว่าบรรทัดนี้เป็น Rewrite Rule
2. Pattern ^old\.html$ เป็นการตรวจสอบ URL ที่ใส่เข้ามาทาง Location Bar ของ Browser ซึ่งจะใช้ regex (Regular Expression) ในการตรวจสอบ
3. Rewrite new.html เป็น URL ที่เราต้องการให้แปลงเป็นอันใหม่
- เครื่องหมาย The caret หรือ ^ เป็นการกำหนดจุดเริ่มต้น URL ที่อยู่ภายใต้ direcory ปัจจุบันที่เราจะตรวจสอบ
ซึ่งก็คือ directory เดียวกับไฟล์ .htaccess
- เครื่องหมาย The dollar sign หรือ $ ซึ่งบอกจุดสิ้นสุดของการตรวจสอบ Pattern
- . เป็นอักขระพิเศษจึงต้องใส่ / นำหน้า
การใช้ Regular Expressions
ตัวอย่างการใช้ regx :
RewriteRule ^kb/([0-9][0-9])$ knowledgebase.php?ID=$1
จะเปลี่ยนจาก knowledgebase.php?ID=47 เป็น kb/47
- () คือ back-reference เป็นการเก็บค่าที่เราตรวจสอบพบ และตรงตามเงื่อนไข ซึ่งเราสามารถเอาค่านี้
ส่งไปให้ PHP ในส่วนของ Query String ได้
สามารถมี back-reference ได้หลายอันใน url อันเดียว
โดย $1 หมายถึงวงเล็บแรก $2 หมายถึงวงที่สองไปเรื่อยๆ
- [] เป็น range เช่น [0-9] จะใช้ตรวจสอบว่าเป็นตัวอักษรที่อยู่ในช่วง 0-9 ใดๆ
[A-Z] ตรวจสอบว่าเป็นตัวอักษรตัวใหญ่ตั้งแต่ A-Z
- ในกรณีที่ ตัวเลขหลัง kb มีได้มากกว่า 2 หลักให้เราใส่
Match Quantifiers + ดังนี้
RewriteRule ^kb/([0-9]+)$ knowledgebase.php?ID=$1
- [0-9]+ เป็นการตรวจสอบตัวเลข 0-9 ตั้งแต่ 1 หลักขึ้นไป
Regular expressions
Some hints about the syntax of regular expressions:
Text:
. Any single character
[chars] One of chars
[^chars] None of chars
text1|text2 text1 or text2
Quantifiers:
? 0 or 1 of the preceding text
* 0 or N of the preceding text (N > 0)
+ 1 or N of the preceding text (N > 1)
Grouping:
(text) Grouping of text
Anchors:
^ Start of line anchor
$ End of line anchor
Escaping:
\ char escape that particular char
Pattern Matching metacharacter Definitions
\ Use before any of the following characters to escape or null the meaning or it. \* \. \$ \+ \[ \]
^ Start matching at this point
$ End point of the match
. Any character
[] Starts a range
| Starts alternative match this|that would mean match this or that
() starts a back reference point
? match 0 or 1 time Quantifier
+ match atleast 1 or more times Quantifier
* match 0 to infinite times Quantifier
{} match minimum to maximum Quantifier {0,3} match up to 3 times
Range Definitions []
^ Negates the class. [^A-Z]+ means don’t match any uppercases
\ Use before any of the following characters to escape or null the meaning or it. [\+]+
- Range for matching [0-9]+ [a-zA-Z]+
Command Flag
[R] Redirect you can add an =301 or =302 to change the type.
[F] Forces the url to be forbidden. 403 header
[G] Forces the url to be gone 401 header
[L] Last rule. (You should use this on all your rules that don’t link together)
[N] Next round. Rerun the rules again from the start
[C] Chains a rewrite rule together with the next rule.
[T] use T=MIME-type to force the file to be a mime type
[NS] Use if no sub request is requested
[NC] Makes the rule case INsensitive
[QSA] Query String Append use to add to an existing query string
[NE] Turns of normal escapes that are default in the rewriterule
[PT] Pass through to the handler (together with mod alias) Skip the next rule S=3 skips the next 3 rules
[E] E=var sets an enviromental variable that can be called by other rules