How to url rewriting in php using htaccess?

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:-

http://example.com/example.php?x=xx&y=yy

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:- http://example.com/example/xx/yy
These type of url easier to read, remember and much clean. we can also do more effort to make it more clean.

Requirement:-

  • 1. mod_rewrite must be enabled to Apache server.
  • 2. Need to create a .htaccess file.
  • Turn On Rewrite Engine :-
    RewriteEngine On

    1. Remove .php extension from url :-

    To remove .php extension like if you want change http://example.com/myfile.php to http://example.com/myfile 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 http://www.example.com/myfile.php to http://www.example.com/something

    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



    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 http://www.example.com/myfile.php?id=7 and want to change http://www.example.com/myfile/7 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


    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




  • Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here