Jump to content

Super simple URL rewriting tutorial with .htaccess


Stefany93

Recommended Posts

Note: This article assumes you have a basic understanding as to what .htaccess files
are, how they work and how to create one. And that you know how to use regular expressions.


When we were green programmers, we would create a website with these files:

    

index.php about.php contact.php

And when our visitors wanted to access a certain web page within our website, we would
provide them with links to these documents directly like this:
    

www.example.com/about.php

However, all this above turned out to be the wrong way for many reasons. First of all,
referencing web pages with their full name is not very secure because an evil user would know
(a where "about.php" is located, b) that it is a PHP file and that will facilitate his work to ruin our website.
The correct way to fix this is with a .htaccess file placed within out root directory where our website resides.
Before you use your .htaccess file to re-write an URL, make sure your server a) supports .htaccess files because some
bosses of hostings forbid them for God knows what reasons and b) that even if the hosting allows .htaccess files, you might
need to contact the hosting people to ask them to configure their server to accept URL re-write on a per directory basis.

Now we would want to turn this URL http://www.example.com/about.php into http://www.example.com/about
Our very first directive must be:

    

RewriteEngine On # We are turning on the rewrite engine on our server

Some hostings require you to specify as to from where the re-writing shall begin.
Let's say you store all of your website files and folders in your root directory.
Then your second directive will be:

    

RewriteBase / # We shall be re-writing files in our root directory.

    
When testing on localhost using the "RewriteBase" directive will give a server error.

Our third directive:

    

RewriteRule about about.php

Now, golden rule - when re-writing a URL, you must first specify the RewriteRule directive and then
the word right after that is the word that rewrites the name of the file whose name you want
to re-write. So RewriteRule about means that the word that will come after "about" is the one
that will henceforth be known as "about" in this case about.php
So:

    

RewriteRule about about.php # Re-write the file name about.php to about

So when we write in the URL http://www.example.com/about the server will actually be pulling the information
from about.php but the user will never know! That will be our little secret!

Now the problem with the above approach is that the re-writed name is not spesific. Meanning that "about"
will match any URL that has the word "about" in, for example:

    

http://www.example.com/aboutmyawesomepizza

The above will be matched. We do not want that. We can solve this problem with regular expressions:

    

RewriteRule ^about/?$ about.php

Super! Now our URL will only match http://www.example.com/about with or without trailing slash, does not matter.

One last thing - we want about URL to be case-insensitive so that http://www.example.com/about and http://www.example.com/AboUt will both
match the about.php file. We do that by putting the [NC] flag at the end of the directive like so:

    

RewriteRule ^about/?$ about.php [NC]

"NC" meanning "no case". I also thought it meant "North Carolina" but sadly it did not.
All directives put together:

    

RewriteEngine On

        RewriteBase /

        RewriteRule ^about/?$ about.php [NC]

More reading:
What are .htaccess files? - http://httpd.apache.org/docs/2.2/howto/htaccess.html
RewriteRule directive - http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule

 

Link to comment
Share on other sites

Your post is informative with basic usage of mod_rewrite however this should of been posted in Apache mod_rewrite forum and not here. This is help with PHP code.

 

Do the note mapping files without file extensions can also be achieved by adding MultiViews to Options flag in .htaccess

Options +MultiViews
Edited by Ch0cu3r
Link to comment
Share on other sites

 

Your post is informative with basic usage of mod_rewrite however this should of been posted in Apache mod_rewrite forum and not here. This is help with PHP code.

 

Do the note mapping files without file extensions can also be achieved by adding MultiViews to Options flag in .htaccess

Options +MultiViews

 

Sorry, I was searching for the correct category and couldn't find one :( If a moderator is so kind as to move the topic that will be great.

 

Thanks for the update.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.