Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Rewrite rules to create user friendly URLS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 608
    Comment on it

    Rewrite Rule is a directive used in .htaccess file to control the routing of the pages in a webservice, webresource or webapplication and this technique is called URL rewriting.

    Apache HTTP Server provides the URL rewritting technique via mod_rewrite module.

    mod_rewrite module provides a powerful way to do URL rewriting.

    For example:
    let us take a url:
    http://www.firstexample.com/example_rewrite_303_2015.php
    we would be looking for more clean url, which can be as below
    http://www.firstexample.com/example-rewrite/
    Now in order to get this url, we tell the server that redirect internally all the requests for the URL
    "http://www.firstexample.com/example_rewrite/"
    To "http://www.firstexample.com/example_rewrite_3
    03_2015.php".

    Now to fulfill the above requirement we use the .htaccess, a text document wherein we mention the rewrite rule.
    The name of the htaccess file is only extention ".htaccess" without any file name.
    We have to place this file in the root directory where we have the file example_rewrite_303_2015.php

    Now we write two rules in the server configuration file .htaccess as follows:
    1. RewriteEngine On # This is for turning rewriting engine on
    2. RewriteRule ^example-rewrite/?$ example_rewrite_3
    03_2015.php [NC,L] # This directs the Apache to handle requests for "example-rewrite"

    The first rule tells the Apache to turn on the rewrite engine.
    The second rule tell the Apache server that we want to use this rule.
    Let us understand the rewrite rule:

    1. RewriteRule -> this is a directive which tells apache server about the rule.
    2. ^example-rewrite/?$ -> this is the pattern of the rule for our specific action. The server will check if any request made on the site matches the pattern . If there is a match, apache will swap the requested URL to the substitution URL i.e. example_rewrite_303_2015.php.
    3. "example_rewrite_3
    03_2015.php" this is a substitution section to which the matched pattern is swapped with.
    4. [NC,L] These are the flags that tell the Apache server that the rule is not case sensitive by first flag "NC" and second flag "L" tells the server that don't use any more rules if this one is used.
    5. # This directs the Apache to handle requests for "example-rewrite" This is a comment section to write about the rule, it is optional.

    Now we take another example
    http://www.firstexample.com/display_my_example.php?example_id=8
    we want the above url to be changed to the below one:
    http://www.firstexample.com/my_example/8/

    Now instead of writing rules for every single example id we must write a rule to manage the url for all the examples dynamically so that we can get the following pattern for every example.
    http://www.firstexample.com/my_example/number/
    For this to be done we use regular expressions. Now to get any number we need a pattern for any number in regular expression so we use a pattern i.e. [0-9]+



    Elaborating "[0-9]+"
    1. "[]" square brackets give us a range of characters, here the range is any number from 0 to 9.
    2. "+" one or more number that will precede plus sign.
    2. "+" one or more number that will precede plus sign.
    RewriteRule ^my_example/([0-9]+)/?$ display_my_example.php?example_id=$1 [NC,L] # This directs the Apache to handle requests for "my_example"
    Elaborating the above rule:
    1. ^ this shows the start of the string or "negative" if at the start of a range i.e. [^0-9].
    2. - this represents the range if used between the square brackets. Else this is converted to space.
    3. my_example/(0-9)+/ is a pattern.
    4. ? ungreedy modifier in regular expression. This indicates that the shortest match should be used.
    For example: "c.?d" would match "cd" in "cdcdcd", where "c.d" would match the entire string.
    5. $ this represents the end of the string.
    6. substitution = display_my_example.php?example_id=$id.
    "$id" tell apache to put into the URL that matched the pattern in the bracket. 7. Flags = {NC: not case sencitive} & {L: Last - stop processing rules}
    Hence the RewriteRule below means that Apache redirects all requests

    RewriteRule ^my_example/([0-9]+)/?$ display_my_example.php?example_id=$1 [NC,L] # This directs the Apache to handle requests for "my_example"
    For: "firstexample.com/my_example/{number}/"
    To: "display_my_example.php?example_id={same number}"

    Here you will see another example of rewriting rule for redirecting http to https

    we can write the below line to our httpd.conf file or in our file where we have mentioned our virtual host and we can also write there rules in .htaccess file.

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    

    With the above rewrite rule the server will automatically redirect

    http://www.firstexample.com/mypage.html To https://www.firstexample.com/mypage.html

    We can also redirect a single page say http://www.firstexample.com/reportpage.html To
    https://www.firstexample.com/reportpage.html by using the following rewrite rule in the .htaccess file.

    RewriteEngine On
    RewriteRule ^reportpage.html$ https://www.firstexample.com/reportpage .html [R=301,L]

    Meaning of flags are as below:

    R=301 => Page moved permanently to the url with https:// instead of http:// in the above rule.
    L => Last - stop processing rules further.

    For adding www in the URL we use the following rule in the .htaccess file.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    


    Elaborating the above rule
    1. First rule tells the server to start the Rewrite module.
    2. The second directive specifies that the next rule executes when the HTTP_HOST is equal to example.com.
    3. In the pattern (.) => dot (.) specifies that we can use any one character and () specifies that we can use another character, yet again another character and so on. (^) is the start and ($) is the end of the string so ^(.*)$ contains the requested url without the domain
    4. The Flag R=301 means that the server returns a 301 moved permanently to the requesting browser.
    5. L means this is the last rule in this execution.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: