How understand rewrite engine – htaccess

Overwriting url it is one of the most used techniques used now on days, even tho a lot people does use it everyday, not everyone really understand how do they work, but not for the reasons you may think, but mostly because they do work by default on CMS such WordPress, Joomla, etc..

lets create some files:

  1. .htaccess
  2. index.php

on out htaccess file we need to write the following:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?some=$1 [L,QSA]

Now our index.php:

$request= $_GET['some'];
$params = split("/", $request);
$safe_pages = array("youpage");
if(in_array($params[0], $safe_pages)) {
echo 'ok';
} else {
echo 'not ok';

RewriteRule ^(.*)$ index.php?some=$1 [L,QSA], the parenthesis around .*  takes or get all values and puts them into $1 and later on it is assigned to "some" on ?some=$1. since some it is just a variable you can change the name to anything you want, this variable becomes accessible like this: $_GET['some'];