How to use Rewrite Rules in Apache ?

What are the URL Rewriting ?

Rewriting is used to convert or transform the “complex URLs” to “Simple and  user friendly URLs”.

  • Rewriting URLs can help us to transparently redirecting URLs to other URLs without showing user any information behind it.
  • Redirecting “Complex Urls” to “Simple URLs” .
  • Also used to create useful URLs for SEO process.
  • Redirecting URLs also used for security purpose .

URL Rewriting in Apache Server:-

  • Rewrite  

http://www.xyz.com/abc-data.php

to

http://www.xyz.com/abc-data/

For above redirection we need to add below lines to apache configuration or in .htaccess file.

RewriteEngine On

                RewriteRule  ^/abc-data.php$    /abc-data                           [R,L]

Below are the points describing the above configuration:-

  • First line is used to turn on the Rewrite Engine in apache.
  • Second line has below parts:-

RewriteRule –  It states the single rule to be applied.

^/abc-data.php$  –  This is the pattern which needs to be checked in every request coming to server while parsing the URL . If it matches then the URL will get redirected to the next pattern mentioned below.

/abc-data/  – This is called as substitution. When the above URL matches then apache will use this URL instead of requested URL.

[R,L]  – These are the “FLAGS”, that tell Apache how to apply the rule. Here “R”  is for the temporary redirection and “L” for informing apache about to use this rule as the last rule and not to process any more rule if it has been used.

Below are the FLAGS used in Rewrite Rules.

  • F – Forbidden ( sends a 403 header to the user)
  • L – Last Rule (stops processing further rules)
  • NC – Case  insensitive
  • NE – Do not escape special URL characters in output
  • P – Proxy( apache should grab the remote content specified in the substitution section and return it)
  • PT – Pass Through
  • R – Temporary redirect to new URL
  • R=301 – Permanent redirect to new URL
  • QSA – append query string from request to substituted URL
  • CO – Cookie ( set specified cookie)

 

Regular Expressions:-

 

  • mod_rewrite uses the PCRE  (Perl compatible Regular Expression) vocabulary .
    • . – Matches any single character
    • + – Repeats the previous match one or more times
    • * – Repeats the previous match zero or more times
    • ? – Makes the match optional
    • ^ – Matches the beginning of string
    • $ – Matches the end of string
    • ! – Matches the negative pattern
    • [ ] – Match any of the character
    • { } – Minimum to maximum quantifier
    • [^ ] – Matches any character not specified
    • – Range (when used on square brackets)
    • | – Matches either of string/character (OR).
    • \ – Escape character
    • ( ) – Groups several characters into single unit and captures a match for use in back reference.

 

Server Variables Used During Rewrite Rules:-

 

  • {HTTP_HOST} – Returns the host value in the request url. e.g. www.yahoo.com
  • {REMOTE_ADDR} – Returns the IP address of user e.g. 10.207.6.216
  • {REQUEST_URI} – Returns the path component  of the requested URL e.g /index.html (it excludes the query string )
  • {QUERY_STRING} – Returns the query sting of the requested URL e.g. ?x=5&y=8
  • {REQUEST_METHOD} – Returns the method of requested URL e.g. GET, POST, HEAD
  • {HTTP_USER_AGENT} – Returns the user agent in the incoming request e.g. Mozilla, iPhone
  • {REQUEST_SCHEME} – Returns the scheme of the requested URL e.g. http or https
  • {DOCUMENT_ROOT} – Returns the document root of the webserver e.g /var/www/html
  • {HTTP_REFERER} – Returns the URL from where the requested url is referred
  • {HTTP_COOKIE} – Returns the cookie of the requested URL

{REQUEST_FILNAME} – Returns the filename in the requested URL e.g /var/www/html/index.php

 

Enjoy Rewrite Rules. 🙂

Feel free to ask any questions to us.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *