PHP

SIMPLE PRETTY URL WITH PHP HTACCESS

TURN ON REWRITE ENGINE RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L]

CREATE HTACCESS FILE

01

DO NOT REWRITE EXISTING FILES FOLDERS RewriteCond %{REQUEST_FILENAME} !-f RewriteCond  %{REQUEST_FILENAME} !-d

REDIRECT EVERYTHING TO INDEX.PHP RewriteRule . /index.php [L]

EXTRACT PATH FROM URL $path = parse_url ($_SERVER["REQUEST_URI"], PHP_URL_PATH); 

INDEX.PHP PART A

02

REMOVE BASE PATH  $base = "/";  if (substr($path, 0, strlen($base)) ==  $base) {  $path = substr($path, strlen($base)); }

03

EXPLODE INTO ARRAY $path = explode("/", rtrim($path, "/")); 

LOAD REQUESTED PAGE if (count($path)==1) {   $file = $path[0]=="" ? "index.html" :    $path[0] . ".html";  } else {  $file = implode($path, "-") . ".html"; } require $file;

INDEX.PHP PART B