Jump to content

[TUTO] Routeur PHP


Taka

Recommended Posts

Bonjour,

Nativement les serveurs web redirige sur des pages php html ou autre avec des liens ayant comme forme : http://domain.tld/page.php

Le but est de remplacer la partie '/PAGE.PHP' par '/home

Architecture :

--pages
	page.php
	404.php
.htaccess
index.php
routeur.php

 

index.php

<?php 
header('Location: /home'); 
?>

Celui-ci renvoi inconditionnellement vers la page "/home"

routeur.php

<?php
$request_uri = explode('?', $_SERVER['REQUEST_URI'], 2);

switch ($request_uri[0]) {
//Page d'exemple
case '/home':
        require './pages/page.php';
        break;
// Cas inconnu -> 404
default:
        require './pages/404.php';
        break;
}

Nous séparons la variable "$_SERVER['REQUEST_URI']" afin d'en récupérer uniquement le "/XXXXX" "XXXXX" étant notre page recherché dans le cas présent "/home"

 

Notre routeur est fonctionnel, hors afin d'être pleinement opérationnel, il faut maintenant forcer à l'aide d'un ".htaccess" la redirection d'url vers notre routeur.php

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.php [L,QSA]

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Read Terms of Use to continue