How to set apache to use clean url ?
clean url หมายถึงการอ้างถึง url โดยมีลักษณะเหมือนเป็น folder ซึ่ง folder ดังกล่าวไม่ได้มีอยู่จริงใน host ก็จะเกิด error 404
ให้กำหนดชื่อไฟล์ที่ server จะทำการ redirect เมื่อเกิด error 404 มาที่โปรแกรมหลัก ทุกครั้งที่มีการอ้างอิงถึง clean url ก็จะมาที่โปรแกรมหลักของเราเสมอ ในโปรแกรมหลักก็จะตรวจสอบเจ้า folder ที่ส่งมา แล้วทำงานตามที่ได้ตั้งข้อกำหนดเอาไว้
การกำหนด error 404 ได้ในไฟล .htaccess ดังนี้
## Apache/PHP/CMS settings
Customized error messages.
ErrorDocument 404 /index.php
หรือการกำหนดให้ Apache redirect page เมื่อหา page นั้นไม่เจอ
- you must enable mod_rewrite module by remove # before this line in httpd.conf
# Enable mod_rewrite
LoadModule rewritemodule modules/modrewrite.so
2. set apache to allow .htaccess file by change value AllowOverride to All
# AllowOverride controls what directives may be placed in .htaccess files.
AllowOverride All
- and restart Apache
ดังนั้นทุกครั้งที่เรียกมา ก็จะถูกย้ายไปสู่ index.php