basicurlrewitinginphp

Upload: sourabh-bhandari

Post on 08-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 basicurlrewitinginphp

    1/1

    BASIC URL REWRITE WITH MOD_REWRITE

    In this tutorial we will see how to rewrite php url query string like this:

    http://localhost/mytutorials/index.php?a=1&b=2&c=3

    You might have seen many of the urls like one above with lots of symbols and alphabets.I'll show you how to make clean and search engine friendly URL out of this. We willturn this link into:

    http://localhost/mytutorials/1/2/3

    To do this I'll use a module on apache webserver called module rewrite. We will create afile named .htaccess and write the code shown below :

    RewriteEngine OnRewriteBase /mytutorials/

    RewriteRule ([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?a=$1&b=$2&c=$3

    Now what this code does is: First it will check whether the module rewrite is present on

    the server or not. If it is found then it will turn it on and set the base (base is basically theroot directory of the webfiles.). Then I have written the rewrite rule to convert the querystring into desired url. Here ^(carot) means the beginning of the url and $ means the endof url. $1,$2,$3 means argument number 1,2 and 3 respectively.

    Sourabh Bhandari http://apnetutorials.co.cc

    http://localhost/mytutorials/index.php?a=1&b=2&c=3http://localhost/mytutorials/1/2/3http://localhost/mytutorials/index.php?a=1&b=2&c=3http://localhost/mytutorials/1/2/3