【问题标题】:Friendly urls in my PHP application我的 PHP 应用程序中的友好 URL
【发布时间】:2014-01-29 03:40:22
【问题描述】:

我想在 PHP 应用程序中更改我的 URL,以便它使用友好的 URL。

如果我启动它,url = localhost/Inhaalopdracht/MEOK1/MVCthuis/boeken/index.php?author=1

我希望它类似于:localhost/Inhaalopdracht/MEOK1/MVCthuis/boeken/index.php/author/1

这是我的索引:

<?php
//lees de controller
require_once("controller/Controller.php");

//start de controller
$controller = new Controller();
$controller->start();
?>

我的控制器:

<?php
require_once("model/Model.php");

class Controller
{
    public $model;

    public function __construct()
    {
        $this->model = new Model();
    }

    public function start()
    {
        if(!isset($_GET['book']) && !isset($_GET['author']))
        {
            $books = $this->model->getBookList();
            include 'view/booklist.php';
        }
        else if(isset($_GET['book']))
        {
            $book = $this->model->getBook($_GET['book']);
            include 'view/viewbook.php';
        }
        else if(isset($_GET['author']))
        {
            $author = $this->model->getAuthor($_GET['author']);
            $books = $this->model->getBookList();
            include 'view/viewauthor.php';
        }
    }
}

?>

还有一个带有信息的页面的示例:

<html>

<head>
    <title>De boekenlijst </title>
</head>

<body>

<table>
    <tr>
        <th> Titel </th>
        <th> Schrijver </th>
        <th> ISBN </th>
    </tr>

<?php
foreach ($books as $key => $book)
{
  echo '<tr>';
  echo '<td><a href="index.php?book=' . $book->id . '">' . $book->title . '</a></td>';
  echo '<td><a href="index.php?author=' . $book->id . '">' . $book->author . '</a></td>';
  echo '<td>' . $book->isbn . '</td>';
  echo '</tr>';
}
?>
</table>
</body>
</html>

【问题讨论】:

标签: php url mod-rewrite


【解决方案1】:

这可能对您有所帮助。试试这个。

在这种情况下,您必须使用 URL 的 301 重定向。这是一个例子:

重定向

localhost/a1/articles.php?id=1&name=abc

localhost/article/1/abc

使用以下内容修改 .htaccess 文件:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)&name=(.*)$
RewriteRule ^articles\.php$ /article/%1/%2? [R=301]
RewriteRule ^article/([^-]+)/([^-]+)$ /articles.php?article_id=$1&article_name=$2 [L]

【讨论】:

  • 你得到答案了吗...?
【解决方案2】:

首先您需要启用 Apache mod_rewrite 扩展。 然后在服务器的文档根目录中放置一个 .htaccess 文件,其中包含类似这样的内容。它应该可以完成这项工作:

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^Inhaalopdracht/MEOK1/MVCthuis/boeken/index.php/author/?$    Inhaalopdracht/MEOK1/MVCthuis/boeken/index.php?author=$1    [NC,L]

查看教程了解更多详情:http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 2011-12-06
    • 2016-04-13
    • 2011-01-07
    • 1970-01-01
    • 2017-04-12
    • 2012-08-21
    • 1970-01-01
    • 2016-06-02
    相关资源
    最近更新 更多