2016-02-26 34 views
1

在我的index.php中,我根據操作參數控制需要哪個模板。index.php控制器檢查路徑而不是動作參數

$action = isset($_GET['action']) ? $_GET['action'] : ""; 

在我的其他頁面中,我在href中指定了該操作。

<a href=".?action=post&amp;postId=<?php echo $post->id?>"> 

當您將鼠標懸停在鏈接,它會告訴你像

...?action=post&postId=101 

但我不想表現出來這樣的。相反,我想表明它像

.../post/101 

而且在href,我還挺使用

<a href="./post/<?php echo $post->id?>"> 

我的問題是,那麼我將如何能夠處理它在index.php

+1

maybe [this](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained?rq=1)可以幫助 – roullie

+0

所以這意味着我可以保持我的index.php的實現,只需更新.htaccess文件? – iPhoneJavaDev

+0

是的,你可以保持你的執行 – roullie

回答

1

使用的.htaccess

RewriteEngine On 
RewriteRule ^article/([0-9]+)\/?$ /page-to-load.php?action=post&postId=$1 [NC,PT,L] 

有了這個例子中,服務器將採取

www.example.com/article/101

,並將其重定向到www.example.com/page-to-load.php?action=post &帖子ID = 101

而在地址欄中保留www.example.com/article/101

0

有幾種方法可以做到這一點。讓我們將其限制爲每個答案的一種方法:我選擇PATH_INFO。

您可以在https://tools.ietf.org/html/rfc3875#section-4.1.5

基本上web服務器上PATH_INFO的CGI規範的描述讀了起來,決定以服務/要運行的腳本資源後,使通過一個叫做PATH_INFO變量可用的其他路徑。在PHP中,這通常結束於$_SERVER['PATH_INFO'],請參閱http://php.net/manual/en/reserved.variables.server.php

但是,您必須將Web服務器配置爲接受並傳播此信息。
對於httpd apache,這是通過設置AcceptPathInfo On完成的。

對於其他網絡服務器itdone有區別。

相關問題