2017-07-05 24 views
0

我想刪除=和?來自PHP中get方法的URL。我的工作我的網站分頁結構,目前這是什麼網址是這樣的:刪除=和?從PHP獲取方法沒有htaccess

www.example/test/?page=3 

我希望它看起來像這樣:

www.example/test/3 

這是生成的PHP代碼網址。

<?php 
    class Pagination { 
    public $current_page; 
    public $per_page; 
    public $total_count; 
    public $pages_articles; 

    public function __construct($page=1, $per_page=20, $total_count=0) { 
    $this->current_page = (int)$page; 
    $this->per_page = (int)$per_page; 
    $this->total_count = (int)$total_count; 
    $this->pages_articles=array(
    '<div class="article-loop"><img src="http://i.imgur.com/CmU3tnl.jpg"> </div>', 
'<div class="article-loop"><img src="http://i.imgur.com/TDdxS9H.png"></div>', 
'<div class="article-loop"><img src="http://i.imgur.com/39rpmwB.jpg"></div>', 
'<div class="article-loop"><img src="http://i.imgur.com/1lBZQ1B.png"></div>', 
'<div class="article-loop"><img src="https://i.imgur.com/Y5Ld4Qfh.jpg"></div>', 
'<div class="article-loop"><img src="http://i.imgur.com/8HumESY.jpg"></div>', 
'<div class="article-loop"><img src="http://i.imgur.com/CqCZBvk.png"></div>', 
'<div class="article-loop"><img src="http://i.imgur.com/wQVPRVp.png"></div>'); 
} 

    public function offset() { 
return ($this->current_page - 1) * $this->per_page; 
    } 
    public function total_pages() { 

return ceil($this->total_count/$this->per_page); 
    } 
    public function previous_page() { 
return $this->current_page - 1; 
    } 
    public function next_page() { 
return $this->current_page + 1; 
    } 
    public function has_previous_page() { 
return $this->previous_page() >= 1 ? true : false; 
    } 
    public function has_next_page() { 
return $this->next_page() <= $this->total_pages() ? true : false; 
    } 
} 
    $page = !empty($_GET['page']) ? (int)$_GET['page'] : 1; 
    $per_page = 3; 
    $total_count=8; 
    $pagination = new Pagination($page, $per_page, $total_count); 
    ?> 

<html> 
<body> 
<div> 
<?php 
    $i = $pagination->offset() ; 
    $limit = $pagination->per_page; 
    while($i<$pagination->total_count && $limit>0) { 
    echo $pagination->pages_articles[$i]."<br>"; 
    $i++; 
    $limit--; 
    } 
    ?> 
    </div> 
    <ul> 
    <?php 
    if($pagination->has_previous_page()) { 
    echo '<li style="display:inline"><a href="index.php?page='.$pagination->previous_page().'">&laquo;</a></li>'; 
    } else { 
    echo '<li style="display:inline" class="disabled"><a href="#">&laquo; </a></li>'; 
    } 
    for($i=1; $i<=$pagination->total_pages(); $i++) { 
    echo '<a href="index.php?page='.$i.'"><li style="display:inline; margin-left:5px; margin-right:5px">'.$i.'</li></a>'; 
    } 
    if($pagination->has_next_page()) { 
    echo '<li style="display:inline"><a href="index.php?page='.$pagination->next_page().'">&raquo;</a></li>'; 
    } else { 
    echo '<li style="display:inline" class="disabled"><a href="#">&raquo;</a></li>'; 
    } 
    ?> 
</ul> 
</body> 
</html> 

https://3v4l.org/i4GZQ

+3

您需要嘗試**自己編寫代碼**。在[**做更多的研究**之後](https://meta.stackoverflow.com/q/261592/1011527)如果你有問題**發佈你已經嘗試了**的**明確的解釋不工作**並提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。閱讀[如何問](http://stackoverflow.com/help/how-to-ask)一個很好的問題。請務必[參觀](http://stackoverflow.com/tour)並閱讀[this](https://meta.stackoverflow.com/q/347937/1011527)。 –

+0

請將代碼添加到問題正文中,而不是作爲可能過期的外部鏈接,可能位於已關閉或最終刪除的網站上,或由未知的第三方控制。您可以編輯您的問題,添加與手頭問題相關的任何代碼,以幫助人們調查,複製和解決您的問題。 – tadman

+0

框架通過路由層來完成這個任務,並且該路由層依賴於使用'.htacess'類型規則來將所有內容重寫爲'index.php'。這是沒有辦法的。這就是你的服務器的工作原理。如果它沒有被配置爲明白'/ test/3'實際上是對這個文字路徑之外的東西的引用,那麼它就只有404個。 – tadman

回答

1

您的Web服務器必須把這個URL映射到你的PHP程序莫名其妙

傳統方法是使用.php文件擴展名並將URL直接映射到它上面,但這不會產生您想要的效果。

爲了達到這個效果;使用mod_rewrite是常見的,並且將mod_rewrite指令放在.htaccess文件中也很常見。這是the Apache manual有什麼看法:

你應該避免使用完全.htaccess文件,如果你有機會到主的httpd服務器配置文件。使用.htaccess文件會降低您的Apache http服務器速度。您可以包含在.htaccess文件中的任何指令在目錄塊中設置得更好,因爲它具有相同的效果並具有更好的性能。

所以,你可以遵循的建議,並把你不希望把.htaccess在你的Apache配置文件mod_rewrite指令。

假設您使用的是Apache並有權訪問這些文件。

+0

我該如何用mod_rewrite重寫url?我想隱藏?page = –

+0

https://stackoverflow.com/tags/mod-rewrite/info鏈接到你需要知道的一切。既然你排除了.htaccess,我認爲你已經確定mod_rewrite是一個解決方案,因此在Stackoverflow上找到了許多關於它的指南。 – Quentin

+0

我被告知它會更清潔,通過PHP而不是htaccess。 –