The .htaccess
is actually only 1 half of the job.
This .htaccess
will achieve what you want (I think)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.+)$ index.php?request=$1 [QSA,L]
This .htaccess
turns all your requests into index.php?request=myurl
on the background, while on the front it says mydomain.com/myurl
, however, it will not do this for the extensions as stated on the 1-to last line. (otherwise you’ll get into trouble when trying to load css, js, images, etc). Add any extension you wish to exclude from the routing there.
However, there is still the part of catching this in PHP. Everything will route through your index.php now. This means you’ll need a script to “catch” the request and manually include the files needed.
I’ll paste a very simple example for you here:
<?php
$getRequest = explode("https://stackoverflow.com/", $_GET['request']);
$page = $getRequest[0];
if( file_exists("/mypages/$page.php") ) {
require_once('myheader.php');
require_once("/mypages/$page.php");
require_once('myfooter.php');
}
else{
echo "page not found";
}
?>
6
solved How to rewrite url for Get variable in php [duplicate]