Basics of url rewriting

Rakesh Sharma      No Comments  

Basics of url rewriting with examples for beginners

Introduction :-In this article we will explore some basics of url rewriting. First we need to know what is url rewriting?. Url rewriting means modify url’s appearance. rebuild an url to a new url or make a user or search engine friendly url. url rewrite depends on mod_rewrite must be enabled on Apache server. The main purpose of url rewrite to make clean url most of sites have dynamic variables in url like:-
This is not a readable url or not useful for search engine and also not define to page purpose so using url rewrite we can get a shorter and standard url like:-
These type of url easier to read, remember and much clean. we can also do more effort to make it more clean.


1. mod_rewrite must be enabled to Apache server.
2. Need to create a .htaccess file.

Basics of url rewriting

Turn On Rewrite Engine :-

RewriteEngine On

Basic syntax of url rewriting rule :-

RewriteRule pattern target [Flag1,Flag2,Flag3]

Examples of url rewriting :-

1. Remove .php extension from url :-

To remove .php extension like if you want change to add below code to .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

if you want to give a specfic name to file like to

RewriteEngine On 
RewriteRule ^something/?$ myfile.php [NC,L]

RewriteRule – Tells Apache to applying single RewriteRule.
^something/?$ – Regular expression or pattern. server will check this pattern on each request to redirect instead of substitution
myfile.php – Substitution
[NC,L] – Flags, refer Apache to apply a rule

2. Redirect non www to www url :-

if you want url with www redirection or add a www in url add below code to your .htaccess.

RewriteCond %{HTTP_HOST} ^$
RewriteRule (.*)$1 [R=301,L] 

which redirect to with adding www in url.

3. Dynamic pattern :- rewrite with specific name applied for a single url rewrite for multiple or dynamic url rewriting you need to use regular expressions in pattern like :-
if you using query string in url like and want to change then use

RewriteRule ^myfile/([a-zA-Z0-9]+)/?$ myfile.php?id=$1 [NC,L,QSA] # getting all numeric and alphabet 
// if you want to check with only numeric
RewriteRule ^myfile/([0-9]+)/?$ myfile.php?id=$1 [NC,L,QSA] #applying numeric pattern

Some of meta character used in regular expressions:-

. – any character
* – preceding start from zero
+ – preceding start from one
{} – min to max quantifier
? – ungreedy modifier
^ – must be start
$ – end of string
[] – class, apply more match
– – range if used in class
() – group
| – or
\ – escape character

For More details about regular expressions Follow here

4. Multiple url rewriting :-

sometimes we need to apply rules on more pages or files so we can also done easily with adding more rules in .htaccess

RewriteRule ^myfile/(.*)/(.*)/?$ myfile.php?id=$1&name=$2 [NC,L]
RewriteRule ^secondfile/(.*)/(.*)/?$ secondfile.php?id=$1&name=$2 [NC,L] 

Url rewrite Flags :-

Some of common used url rewrite flags

L – Last, to stop processing no further rules will be processed
N – Next, flag start ruleset again from the top
NC – NOcase, means case-insensitive
R – Redirect
QSA – query string append

For More details about url rewrite flags Follow here

There are more miss to learn about url rewriting but i tried to clear some basics of url rewriting for beginner’s. For more i found two more good tutorials for beginners to learn url rewrite step by step :-

Url Rewriting Guide :- official guide of url rewrite.
URL Rewriting for Beginners :- A ever nice url rewrite guide for beginners.
Now you have learned about basics of url rewriting and you are ready to use some rewrite methods.