Computing Magazine

Rewrite Links with Htaccess

Posted on the 14 September 2012 by Akahgy


Rewrite or redirect links insite a web application.


Redirection is simple using htaccess. Just create a file named .htaccess in the root of your web project and with some basic rules and regular expressions, all calls to your webiste can be redirected.


RewriteEngine on
RewriteRule ^item-([0-9]+)\.html$ item.php?id=$1 [NC]
RewriteRule ^(.*)\.jpg$ /view.php?image=$1 [NC]

First we enable the rewriting mode, and then start defining the rules. First example will take a link of sort “item-xx” where “xx” is a number and convert it to a link of the type “item.php?id=xx”. Second example searches for links that contain a jpeg image and convert the image name into a parameter for the URL.

The flag at the end, [NC], defines that the link will be considered regardless of the case. Other flags can be defined as well, a full list can be seen here.


Back to Featured Articles on Logo Paperblog