The first step is to create a .htaccess file, which should contain the following code and be saved in the same folder as you intend to save the index.php file
RewriteEngine on RewriteRule ^([a-zA-Z0-9]+|)/?$ index.php?name=$1 # Handle requests for "index.php"
Now create the index.php file and insert the following
<?php echo $_GET['name']; ?>
Now it's time to test
You'll need to be running something like MAMP or have this code on your server. Type the first part of the URL so that you are pointing the web browser at the folder that contains the two files you created. Next type after the forward slash a word, e.g.
Note: Here my files were saved in a folder called rewrite (and I was using MAMP).
It's all very well having fixed positions in the URL relating to query terms, but sometimes we'll want the added flexibility of also accepting regular query strings, e.g.
If we tried processing this at present, the query string would be ignored. To stop this from happening we need to add [QSA], which appends any query string from the original request URL to any query string created in the rewrite target.
RewriteRule ^([a-zA-Z0-9]+|)/?$ index.php?name=$1 [QSA] # Handle requests for "index.php"
Now replace the index.php code with the following:
<?php if (isset($_GET['name'])) echo "Hello ".$_GET['name'].", "; if (isset($_GET['age'])) echo "you look really young for ".$_GET['age']; ?>
Another handy thing to know is that if we add [NC] to the end of the RewriteRule then it becomes case insensitive:
RewriteEngine on RewriteRule ^([a-z0-9]+|)/?$ index.php?name=$1 [QSA,NC] # Handles requests for "index.php"
For further discussion of ReWrite Rule flags (e.g. QSA, NC), see http://httpd.apache.org/docs/current/rewrite/flags.html
I discuss at further length rewriting here: http://sketchytech.blogspot.co.uk/2013/01/super-simple-htaccess-rewrite-to-create.html