Diffrence between URL rewriting and URL redirection htaccess topic



URL rewriting and URL redirection are two different terms used to map a URL from one location to the other. These two terms often confuse new users especially when they refer to URL related queries. Some users even intermix the URL rewriting logic with URL redirection like they are talking about a particular thing with two different languages. I often see such questions on StackOverflow and encourage posters to fix it.

In this post I will give you a clear overview of what these two terms actually mean and how we use them with URLs.



URL redirection



URL redirection is when an URL redirects from one location to the other and the appriance of URL changes in browser address bar from old to new (from the typed URL to the new location). URL redirction happens client side . Server gives a command to your web browser to change the URL to a new location. Lets understand with a real world example : if you click on this link my server will send a redirect command to your browser to go from this page to example.com.



Notice how the URL changes in your browser address bar to example.com. This redirection is also called external redirection or transpirant redirection as its visible to your web browser and your browser knows whats happening on the screen. On apache server you can create a Redirect either by using a file based method in html, PHP or using Redirect commands in htaccess or server.config file. Apache server provides multiple methods to redirect URLs by htaccess. There are 2 diffrent modules that you can use for URL redirection on apache Mod-alias and mod-rewrite both modules support htaccess and server.config context.





4 ways to redirect URLs on apache server


Given bellow are the common methods to redirect URLs from a website.


  1. HTML:
    HTML anchor tag (a) is used to redirect the currunt page URL to a specific location

    <a href="http://example.com">redirect to example.com"</a>



  2. PHP:
    Redirecting the current page to example.com

    <?php
    header("location:https://example.com/");
    ?>



  3. Redirecting a specific path to example.com using Redirect directive in htaccess:

    Redirect /path http://example.com/


  4. Redirecting a specific path to example.com using RewriteRule in htaccess:


    RewriteEngine on
    RewriteRule ^thispath http://example.com [R]










URL rewriting


URL rewriting is way of rewriting urls. URL rewriting is When a URL path internally maps to another path on server. By "internally" I mean an "Invisible URL redirection" . You and your browser can't see how the url changes from Old to new as it starts and ends behind the screen and your browser stays on the same URL. URL rewriting is server side only as it happens on a webserver without informing the client of the redirection. You get the web page from new location on the same (typed) url as it doesn't change from old to the new one.
URL rewriting is also called internal URL or invisible url redirection as its invisible to a web browser only the webserver executes this redirection and sends the contents of the new page to browser. For example the following url http://example.com/file can be rewritten to http://example.com/file.php by using a RewriteRule directive on apache server. And then instead of typing /file.php you can simply type the new url /file your server will internally redirect/rewrite it to /file.php serving you the contents of /file.php on the /file URI.



Rewriting URLs on apache server



Apache server provides mod-rewrite module for rewriting URLs. In order to use this module you must first enable it on your server if it isnt already enabled.
There are hundreds of URLs rewriting basic examples on Apache guide if you are new you can read their complete guide to understand how it works . To rewrite URLs on apache server, you can either use your config file or your htaccess to write rules.
URL rewriting is done by using RewriteRule directive of mod-rewrite module. The following is a basic example of how we use RewriteRule in an htaccess file. The rule rewrites /a.php to /b.php .

RewriteEngine on
RewriteRule ^a.php$ /b.php [L]


Post a Comment

0 Comments