I have never used .htaccess is my life now I am learning it but I find it very difficult to percieve.
Can anyone tell me steps to rewrite url
For instance
From this
Www.example.com?s=abc
To this
Www.example.com/abc
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('/', $_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";
}
?>