เขียน .htaccess ไฟล์เดียวครอบจักรวาล
หน้าแรก SEO เขียน .htaccess ไฟล์เดียวครอบจักรวาล
code ที่ทำให้สร้าง URL ขอเราสวย ๆ ดีต่อการจัดการ และ seo ด้วย มาดูกันว่าเขียนอย่างไรกันบ้าง
โค๊ด: (.htaccess)
| Code |
| <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> |
โค๊ด: (index.php)
| Code |
| <?php $segment = isset($_SERVER['PATH_INFO']) ? explode('/',preg_replace('~^/?(.*?)/? $~','$1',$_SERVER['PATH_INFO'])) : array() ; print_r($segment); print_r($_GET); |
ลองเข้าด้วย
domain.com/class/method/param1/param2?author=Tee
domain.com/class/method/param1/param2/?author=Tee
domain.com/class/method/param1/param2///////////?author=Tee
มันน่าจะออกมาเป็นแบบนี้
โค๊ด:
| Code |
| Array ( [0] => class [1] => method [2] => param1 [3] => param2 ) Array ( [author] => Tee ) |
แบบนี้จะสามารถใช้งานในกรณีที่เราเขียนโปรแกรมไว้ใน Subfolder ได้ด้วย ไฟล์ต่างๆที่มีอยู่จริงบนก็จะไม่ถูก rewrite ไปด้วย สามารถมองเห็นได้
ปกติ แต่ถ้าไม่ปกติแสดงว่าเป็นที่โฮสท์ ซวยไป ต้องเขียน RewriteCond ดักเพิ่มเป็น case ไป
ปล. ถ้า Query String ไม่สามารถใช้งานได้ ที่ .htaccess ลองเปลี่ยน [L] เป็น [L,QSA]
หมายเหตุ
- หากอยาก get ค่า URL เป็นภาษาไทยใช้ rawurlencode(), rawurldecode() เข้ามาช่วย
จาก
http://www.thaiseoboard.com/index.php/topic,19829.0.html
ขึ้นไปด้านบน
