相信大多數的網址都不想要讓使用者直接看到網站的副檔名(ex:http://test.com/index.php),而我們希望使用者看到的網址是:http://test.com/Index。
這時,就需要動些小手腳了。
先從.htaccess這個檔案下手吧。
先在網址的根目錄下新增一個.htaccess檔,然後打開它
<IfModule mod_rewrite.c>
#把rewrite功能開啟
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ load.php/$1 [QSA,PT,L]
</IfModule>
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
#所有網址都要直接執行load.php,可自行修改為自己的預先執行檔
DirectoryIndex load.php
order deny,allow
如果想自行撰寫一個更詳細的.htaccess檔的話可以到#把rewrite功能開啟
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ load.php/$1 [QSA,PT,L]
</IfModule>
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
#所有網址都要直接執行load.php,可自行修改為自己的預先執行檔
DirectoryIndex load.php
order deny,allow
http://www.htaccesseditor.com/tc.shtml
自行撰寫
所以接下來所有的網址(被server看到的網址)都會變成
http://test.com/load.php/test
http://test.com/load.php/admin
而在瀏覽器看到的會是
http://test.com/test
http://test.com/admin
我們現在需要的就是在load.php之後的網址啦~
先在根目錄下新增一個load.php。
再來,開啟load.php,在裡面新增
<?php
if(!isset($_SESSION))
session_start();
$UrlBase = explode('load.php',$_SERVER['PHP_SELF']);
$url = explode('/',$UrlBase[1]);
print_r($url);
?>
如此就可以把load.php之後的網址都抓進$url這個array裡了if(!isset($_SESSION))
session_start();
$UrlBase = explode('load.php',$_SERVER['PHP_SELF']);
$url = explode('/',$UrlBase[1]);
print_r($url);
?>
再來就可以把這個$url丟到你想處理的function去做判斷了
撰寫的程式碼請至
https://github.com/SamLaio/framwork_exp
v0.1版
沒有留言:
張貼留言