2010-08-24 43 views
0

我可以通過使用?cat的值,例如6重寫一個URL,並檢查我的MySQL數據庫使用PHP MySQL的&如果是如何的價值6類別名稱htmlPHP和MySQL URL重寫問題

當前網址。

http://localhost/index.php?cat=6&sub1=8&sub2=24&sub3=81 

在瀏覽器中顯示的新搜索友好URL。

http://localhost/html/basics/forms/select-tag/ 

回答

0

結賬mod_rewrite

您需要打開.htaccess文件(如果它不存在,創建它)幷包含以下行。請注意,RewriteEngine On應該只寫入一次。

RewriteEngine On # Turn on the rewriting engine 
RewriteRule ^html/basics/forms/select-tag/([0-9]+)/?$ index.php?cat=$1 [NC,L] 
0

是的。基本上你會想通過使用mod_rewrite通過路由器腳本傳遞整個url。 .htaccess文件應該是這樣的:

RewriteEngine On 
RewriteBase/ 

#if it's an existing directory 
RewriteCond %{REQUEST_FILENAME} -d [OR] 
#...or an existing file 
RewriteCond %{REQUEST_FILENAME} -f 
#serve it up 
RewriteRule ^(.+) - [PT,L] 

#else, direct to index.php 
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 

然後,在index.php中就可以有這樣的事情:

$request = explode('/', $_GET['url']); 

這將有$請求陣列中的所有你的淘網址片段。一旦你有這些值,你可以連接到數據庫,找到網址代表,並輸出頁面。如果找不到,則可以發送帶有「找不到頁面」消息的404頭。

header('HTTP/1.0 404 Not Found'); 
echo '<p>Page not found</p>'; 

這就是基本的乾淨網址技術。如果URL是較早的,狼藉一片,你只需添加一些邏輯以檢查,並重定向到正確的清潔網址:

if(isset($_GET['cat'])){ 
    //put some logic here 

    header("Location: http://localhost/".$the_clean_url); 
} 

這個基本技術應該有一些修修補補解決您的問題。