PART 2::::เขียน .htaccess แบบ ไฟล์เดียวครอบจักรวาล by Tee++;

เริ่มโดย Tee++;, 07 ธันวาคม 2007, 23:48:19

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

Tee++;

เอาละครับจากที่เคยเขียนไว้ .htaccess ไฟล์เดียวทำงานได้หมด โดยการ get segment ซึ่งเป็นวิธีการเขียนให้ทุก url วิ่งเข้าหา index ทั้งหมดแล้วเอา segment มาใช้งาน
   
PART 1::::เขียน .htaccess แบบ ไฟล์เดียวครอบจักรวาล by Tee++;

วันนี้เราจะมาลองเขียนตรงข้าม คือ แทนที่จะเรียกทุก url เข้าหา index เราจะใช้การเขียน .htaccess เพื่อทำการ remove index ออกแทน เพื่อจะได้ url สวยๆ ลองมาดูกันเลยครับ ไม่ยากๆ ^_^

index.php
<?php
//ตั้งค่า url ที่จะให้วิ่งหา ท่าจะแก้ต้องแก้ .htaccess ด้วย
$page_index "index.php"

//หาค่า root folder ที่รันโปรแกรมออกมา
DEFINE('BASE'preg_replace('#'.getenv('DOCUMENT_ROOT').'#'''str_replace(array('\\'$page_index), array('/'''), __FILE__))); 

//เอาค่า root folder ไป replace กับ uri ทั้งหมดที่ get ได้
DEFINE('TAILER'preg_replace('#'.BASE.'#'''getenv('REQUEST_URI')));

//แยก path กับ query string ออกมาใช้งาน
$parse_url parse_url(TAILER);

//explode url ที่ตำแหน่ง / เพื่อกำหนด segment
$uri explode("/"$parse_url['path']);

//parse ค่าของ query string เพื่อให้กลับมาใช้งานได้ตามปกติ
parse_str($parse_url['query']);

//ทดสอบ print segment แต่ละตำแหน่งออกมา
echo "<pre>";
print_r($uri);
echo 
"<pre>";

//ทดสอบว่า query string ใช้งานได้ตามปกติ
echo "<hr />";
echo 
"<strong>Author:</strong> ".$_GET['author'];
?>


.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images)
RewriteRule ^(.*)$ index.php/$1 [L]


----------------------------------------------------------
ตามตัวอย่างข้างบนถ้าผมลองเขียน url แบบนี้
http://www.localhost.com/book/computer/seo_friendly?author=Tee

ตำแหน่งของ segment ที่ผมจะได้ก็คือ

segment ที่ 0 = book
segment ที่ 1 = computer
segment ที่ 2 = seo_friendly

โดยที่ query string ผมก้ยังให้ค่าตามปกติคือ

$author จะให้ค่า Tee ออกมา

เท่านี้เองก้จะได้ url สวยๆ แบบง่ายๆ แล้ว  :)
[direct=http://laravel.in.th]Laravel in Thai[/direct]
[direct=http://jquerytips.com]jQueryTips by Tee++;[/direct]
[direct=https://www.facebook.com/jQueryTips]jQueryTips Page[/direct]
[direct=https://www.facebook.com/Laravel.in.th]Laravel in Thai Page[/direct]
[direct=https://twitter.com/Teepluss]Teepluss's Twitter[/direct]


Step9

[direct=http://www.thaiseoboard.com/index.php/topic,315485]รับจ้างโพสเว็บ ขายสินค้าและบริการ, ขายบ้าน อสังหาริมทรัพย์[/direct] | [direct=http://www.thaiseoboard.com/index.php/topic,372074]ขายอะไรดี คำแนะนำจาก คุรุเทพ[/direct]
  |  [direct=http://www.iseehomecomplex.com/]ถ้าเพียงเรา.. มีบ้านอยู่...[/direct]

Tee++;

ลืมบอกไปอีกนิดนึง ว่าจะใช้ ไอ้ segment ต่างๆ ยังไง  :P

คือเราเรียกไฟล์ เข้า index.php หมดเลยตามปกติ segment ต่างๆควรจะเรียงตามนี้ครับ

$uri[0] = include and new opj
$url[1] = method
$url[2] or more = parameters

เช่น
<?php
if (file_exists($uri[0]))
{
  
//include ไฟล์ที่มี class ของ module นี้เข้ามา
  
require_once($uri[0]);
  if (
class_exists($uri[0]))
  {
    
//สร้าง new object
    
$obj = new $uri[0];
    if (
method_exists($uri[1]))
    {
       
//slice class กับ method ออกไป
       
$uri_slice array_slice($uri2); 

       
$params = array();
       
       
//จับ uri ตั้งแต่ segment ที่ 2 มา merge ใน array (เพราะ 0 กับ 1 คือ class กับ method ที่เอาออกไปแล้ว)
       
$params array_merge($params$uri_slice); 
       
       
//เรียก method ใน object รวมทั้งส่ง parameters ที่เป็น array ไปด้วย (ถ้ามี)
       
$obj->$uri[1]($params); 
    }
  }
}
?>


แต่ถ้าไม่ถนัดการเขียนโคดแบบ oop ก็ใช้วิธีง่ายๆคือ


<?php
if ($uri[0] == 'module')
{
  require_once(
'module.php');
}
?>


แล้วไปทำงานต่อใน module.php เช่น


<?php
if($uri[1] == 'addnew')
{
  
//โคดโลด
}
?>
[direct=http://laravel.in.th]Laravel in Thai[/direct]
[direct=http://jquerytips.com]jQueryTips by Tee++;[/direct]
[direct=https://www.facebook.com/jQueryTips]jQueryTips Page[/direct]
[direct=https://www.facebook.com/Laravel.in.th]Laravel in Thai Page[/direct]
[direct=https://twitter.com/Teepluss]Teepluss's Twitter[/direct]



Tee++;

เอาไหนๆ เริ่มแล้วขอต่อให้จบเลยละกัน เรื่อง class เช่น สมมุติ เรามี segment ทั้งหมดดังนี้

$uri[0] = "someclass";
$uri[1] = "somemethod";
$uri[2] = "param_1";
$uri[3] = "param_2";

ดังนั้นมันจะมาได้จาก url คล้ายๆ แบบนี้

http://www.domain.com/someclass/somemethod/param_1/param_2
---------------------------------------------------
class ที่เราจะสร้างควรจะมีลักษณะดังนี้

someclass.php
<?php
class someclass {
  function 
somemethod($params)
  {
    
print_r($params); //ค่าที่ได้ควรจะเป็น params_1, params_2
  
}
}
?>

[direct=http://laravel.in.th]Laravel in Thai[/direct]
[direct=http://jquerytips.com]jQueryTips by Tee++;[/direct]
[direct=https://www.facebook.com/jQueryTips]jQueryTips Page[/direct]
[direct=https://www.facebook.com/Laravel.in.th]Laravel in Thai Page[/direct]
[direct=https://twitter.com/Teepluss]Teepluss's Twitter[/direct]


L


intaniato

เฮียตี๋ไปดูบอร์ดโน้นบ้างเด้อครับ คนหายโหม๊ดด

gootum


komgrit_su


hurahura

ช่วยด้วยครับ ใช้กับ host monster ไม่ได้ครับ


ลองในเครื่อง
http://localhost/myweb/book/computer/seo_friendly?author=Tee

Array
(
    [0] => book
    [1] => computer
    [2] => seo_friendly
)
--------------------------------------------------------------------------------Author: Tee


ลองบนเว็บ
http://www.myweb.com/book/computer/seo_friendly?author=Tee

Array
(
    [0] => bookcomputerseo_friendly
)
--------------------------------------------------------------------------------Author: Tee



มันไม่ยอมแยก array ให้ อ่ะครับ

hurahura


spiceday


iceman-cool

อ้างถึงจาก: hurahura ใน 08 มกราคม 2008, 18:10:33
ช่วยด้วยครับ ใช้กับ host monster ไม่ได้ครับ


ลองในเครื่อง
http://localhost/myweb/book/computer/seo_friendly?author=Tee

Array
(
    [0] => book
    [1] => computer
    [2] => seo_friendly
)
--------------------------------------------------------------------------------Author: Tee


ลองบนเว็บ
http://www.myweb.com/book/computer/seo_friendly?author=Tee

Array
(
    [0] => bookcomputerseo_friendly
)
--------------------------------------------------------------------------------Author: Tee



มันไม่ยอมแยก array ให้ อ่ะครับ

ผมก็เป็นเหมือนกันครับ พอขึ้นโฮสจริงแล้ว มันไม่แยกให้อ่ะคับ ท่านไหนทำได้แล้วช่วยบอกหน่อยน่ะครับ ขอบคุณมากครับผม

iceman-cool


limingxin

ตามหามานานแล้วครับสุดยอดจริง ๆ หาอะไรก็เจอในบอร์ดนี้ ;D

ioff

 :o  สุดยอดค่ะ  กำลังงง กับ เจ้าตัวนี้อยู่เลย แล้วถ้ากรณีแบบนี้หล่ะค่ะ  จะแก้ไขยังไง  รบกวนด้วยนะค่ะ 
(มึน มาก ทาง ออก คือ ท่องยุทธภพ เสียวบอร์ดต่อไป)  8)

http://www.thaiseoboard.com/index.php/topic,57917.msg725003.html#msg725003

www.thaihoro.co.cc

mafiary


modicate

อนิจจัง ทุกขัง อนัตตา

uncorp

อ้างถึงจาก: Tee++; ใน 08 ธันวาคม 2007, 00:56:37
เอาไหนๆ เริ่มแล้วขอต่อให้จบเลยละกัน เรื่อง class เช่น สมมุติ เรามี segment ทั้งหมดดังนี้

$uri[0] = "someclass";
$uri[1] = "somemethod";
$uri[2] = "param_1";
$uri[3] = "param_2";

ดังนั้นมันจะมาได้จาก url คล้ายๆ แบบนี้

http://www.domain.com/someclass/somemethod/param_1/param_2
---------------------------------------------------
class ที่เราจะสร้างควรจะมีลักษณะดังนี้

someclass.php
<?php
class someclass {
  function 
somemethod($params)
  {
    
print_r($params); //ค่าที่ได้ควรจะเป็น params_1, params_2
  
}
}
?>




แต่ถ้าผู้ใช้บางคนพิมพ์ url แบบนี้
http://www.domain.com/someclass/somemethod/param_1/param_2/

css และ images มันมองไม่เห็นเลยอะ แก้ยังไงดีคับ

ball6847

โห โค้ดนี้พี่ตี๋เค้าคงไม่ใช้แล้วมั้งคับ กี่ปีแล้วเนี่ย คงไปเปลี่ยนไปใช้ CodeIgniter แทนแล้ว

ลองใช้โค้ดนี้ดูแทนละกันคับ

โค้ด (.htaccess) เลือก

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>


โค้ด (index.php) เลือก
<?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

มันน่าจะออกมาเป็นแบบนี้


Array
(
   [0] => class
   [1] => method
   [2] => param1
   [3] => param2
)
Array
(
   [author] => Tee
)



แบบนี้จะสามารถใช้งานในกรณีที่เราเขียนโปรแกรมไว้ใน Subfolder ได้ด้วย ไฟล์ต่างๆที่มีอยู่จริงบนก็จะไม่ถูก rewrite ไปด้วย สามารถมองเห็นได้ปกติ แต่ถ้าไม่ปกติแสดงว่าเป็นที่โฮสท์ ซวยไป ต้องเขียน RewriteCond ดักเพิ่มเป็น case ไป

ปล. ถ้า Query String ไม่สามารถใช้งานได้ ที่ .htaccess ลองเปลี่ยน [L] เป็น [L,QSA]

คาราวะอาจารย์ตี๋หนึ่งจอก +1
We use Ubuntu.

[direct=http://ng-seo.sourcelab.xyz/]AngularJS SEO Experimental[/direct]