apache的.htaccess很少有工作 - 大部分邏輯都在php本身。 Apache只是將每個請求重定向到選定的腳本文件。
我假設你使用apache wid mod_rewrite。如果是這樣的話,你應該添加這.htaccess文件:
# check if mod_rewrite is present
<IfModule mod_rewrite.c>
#turns it on
RewriteEngine on
#if the requested url isn't a file or a dir
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#process index.php, no matter what was in the url
RewriteRule^index.php [L]
</IfModule>
(注意,也的.htaccess必須有權覆蓋apache的當前設置),你可以查閱一下網址的index.php
在sequested和處理信息
$whatTheUserRequested=$_SERVER['REQUEST_URI'];
$parameterArray=explode('/', $whatTheUserRequested);
//check the first param.
switch($parameterArray[1])
{
//if it was something like "thesite.com/main/fsadf/hgfdsgsdf/...."
case "main":
include "mainPage.php";
//....or do something else
break;
//if it was something like "thesite.com/login/asdfe/xxxx/...."
case "login":
include "loginPage.php";
//....or do something else
break;
default:
//here you MUST check if $parameterArray[1] is a username
if(check in db if $parameterArray[1] is user)
{
include "userPage.php";
}
else
{
include "pageNotFound.html"
};
}
您也可以檢查在包括PHP的第三或第四參數,來處理URL的像「mysite.com/johnny95/edit」
您可以摹基於您希望的任何邏輯來嵌入唯一的url。使用'.htaccess'來重定向發送一個可以在你的數據庫中查詢的參數。你可以找到[這裏]的信息(http://stackoverflow.com/questions/1980721/htaccess-mod-rewrite-automatically-using-php-or-something?rq=1)。我會遠離基於狀態的目錄結構 - 人們移動和移動目錄似乎不值得付出努力。保持簡潔的平面目錄結構。 – mseifert
謝謝@mseifert的迴應。這與StackOverflow與他們的問題URL一樣嗎? – CFitz
我已回覆回覆格式。見下文。 – mseifert