1

I want to change my URLs in a PHP application so that it uses friendly urls.

If i start it the url = localhost/Inhaalopdracht/MEOK1/MVCthuis/boeken/index.php?author=1

I want it to be something like: localhost/Inhaalopdracht/MEOK1/MVCthuis/boeken/index.php/author/1

This is my index:

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

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

My controller:

<?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';
        }
    }
}

?>

And an example of one of the pages with info:

<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>
Ryan
  • 3,493
  • 1
  • 18
  • 38

2 Answers2

0

This might be helpful for you. Try this one.

In this case you have to use a 301 Redirect of URLS. Here is an example:

Redirect

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

to

localhost/article/1/abc

Modify the .htaccess file with the following:

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]
Mukilan R
  • 385
  • 3
  • 16
0

First you need to enable the Apache mod_rewrite extension. And then to place a .htaccess file in the document root directory of your server, which contains something like this. It should do the job:

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]

Check the tutorial for further details: http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/