Mod_rewrite on Apache (MRA)

Have you ever seen a link like this:

  • www.supermall.com/product/detail/cam-12.html
  • singsue.wordpress/archive/2006/05/12/suatu_malam_yang_indah.html
  • www.detik.com (link owned by detik.com)
  • www.kompas.com (link owned by kompas.com)

Of course the link above is easier to remember and read by us as visitors, besides search engines like google.com and yahoo.com will index web pages more easily and often. This can also be said as one of the SEO (Search Engine Optimizing) and cleans URL. Surely we wonder how to change a long link to be so simple and do not need to use the structure of a programming language.

We will try to discuss how to change a long link into a very simple and clean URL. If you are using an Apache server then this can be done by adding a module created by Ralf S. Engelschall, namely mod_rewrite (www.engelschall.com/sw/mod_rewrite).

Mod_rewrite is used to change URLs using rewriting engine commands (based on REGEX parser) requested on the fly. To use this module the minimum version required is Apache v 1.2 or later. You can install mod_rewrite as a module on your server.

Install mod_rewrite on Apache

Before installing this module, check whether this module has been installed or not by using phpinfo() if you are using php as web programming. In the Loaded Modules section, you will see what modules are installed, look for the words mod_rewrite if there is then you do not need to install the module, if not then prepare yourself to install it.

The author uses Apache/2.0.58, PHP/5.1.4, WAMP bundle server with Win XP as the operating system so that the configuration will be explained based on the configuration. To install this module, find the httpd.conf file for WAMP located at "C:/wamp/Apache2/conf/" while if you are using Apache binary check "C:/apache/conf/". Open the file with your favorite editor, notepad, wordpad, dreamweaver, etc. Find the line containing mod_rewrite, my httpd.conf file the word mod_rewrite is on line 165.

123#
124# Dynamic Shared Object (DSO) Support
125#
126# To be able to use the functionality of a module which was built as a DSO you
127# have to place corresponding `LoadModule' lines at this location so the
128# directives contained in it are actually available _before_ they are used.
129# Statically compiled modules (those listed by `httpd -l') do not need
130# to be loaded here.
131#
165# LoadModule rewrite_module modules/mod_rewrite.so

Then remove the # sign on that line so that it becomes:

165  LoadModule rewrite_module modules/mod_rewrite.so

Save the httpd.conf file then restart your Apache, then the mod_rewrite module is ready to use. To make sure, check with phpinfo().

Running a Mission

After the module installation is complete, the next step is to make Apache on the fly rewrite our URL as desired, then you have to write the rules in a file called ".htaccess", remember the file name is ".htaccess" not "something.htaccess" or "httaccess.httacces". The .htaccess file in addition to writing mod_rewrite can also be used to protect a file or directory and so on. How to create the file is easy, open your editor and save as with the name ".htaccess".

If you are hosting to a provider usually we are not allowed to use this type of file ".htaccess", then ask your provider to allow it and explain the reason nicely. If you are using on a localserver as a test then the configuration of this file is in https.conf, find the directory location where you put your internet files. My internet files are still in "E: / hasan / internet" while for the initial configuration "C: / apache / htdocs".

253 <Directory "E:/hasan/internet">
267 Options Indexes FollowSymLinks
274 AllowOverride all  #yang harus Anda rubah
280 Order Allow,Deny
281 Allow from all
284 </Directory>

This section is to limit the use of ".htaccess" on the server, for security it is usually set to "None" in this case to test the mod_rewrite function, change it to "All".

After all the preparations have been done then be ready to fight with all your strength. Now open your .htaccess file to check if it can be used and write the code below then save. 

Options +FollowSymLinks
RewriteEngine On
RewriteRule google http://www.google.com/? [R,L]

Call your browser and type localhost/google or www.domainname.com/google, if it immediately redirects to the google.com page, it means your mod_rewrite is running according to plan, now just adjust it according to your needs.

After installing mod_rewrite on Apache, now we will try to practice directly by writing a simple example with a light explanation.

Create a .htaccess file

Open your editor then save with the name .htaccess, remember not file.htaccess or anything else, because it will not be read by Apache.

Writing mod_rewrite rules

This part is something that can be considered "easy" if you are used to it  🙂, but for the sake of simplicity, just follow along. We will try to dissect the code below.

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^categories/$ index.php?category=categories [L]

Options +FollowSymLinks --MultiViews

Add this command at the beginning of each command, but you can also leave it alone.

RewriteEngine On

This command is used to enable or disable the runtime on the rewrite engine. If set to OFF then the module does not execute the runtime. Then set to ON.

RewriteRule

This command is a command that will be executed by the rewrite module, one Rewriterule is one command so we can add it according to our needs, it can be three, four or nine. ^

Start of line anchor, the beginning of the command comment.

categories/

the result of the desired rewriting according to our needs only. It can look very long or simple. In this example categories are considered as directories not a file name, you can change it to a file with the command: categories.html $ End of line anchor, end of command comment.

index.php?category=categories

This is the initial dynamic link that we want to make simple, in this example using PHP.

[L]

Flag according to our needs, starting with [ and ending with ]. "L" means stop the rewriting process and do not rewrite the rule again.

Save the file and upload it to your server, put it in the main directory.

Change the script you have.

This section is a bit tricky, because you have to change your script, so make sure you backup your file first so that when you need it again it is still there and can be used again. Remember to always backup your file before changing.

The links in your script can be in various locations, so you need to know a lot about the programming structure if you are using someone else's script. It's a different story if you are programming it yourself. The links are usually in:

Links to templates file

You have to be observant in looking for links that exist and will be changed. Some developers put their collection of links in the form of templates or collected into one.

Links to source files

Some developers put links in their source files, such as index.php, forum.php, function.php and so on.

Links everywhere

Developers/programmers like this are a bit heavy, because the links are all over the place, contact the programmer for help.

Change the link related to index.php?category=categories to categories/ or categories.html according to the rule. Please open your favorite browser, call localhost/categories or localhost/categories.html according to the rule that has been created. And if you are using a paid server, please call your domain and the location of the file (www.domainname.com/categories or www.domainname.com/categories.html).

Another example

Suppose we have many links like the following:

index.php?category=categories
index.php?category=contact
index.php?category=images
index.php?category=login
index.php?category=logout
index.php?category=new
index.php?category=rss
index.php?category=unpublished

so we can write the rule as

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^ ([a-z]+)/$ index.php?category=$1 [L]

The rewriterule above will change the link after the category into a collection of letters ([az]+), if the number with ([0-9]). So if we type localhost/contact it will be recognized and changed to index.php?category=contact.

Biography of Muh Hasan Tanjung

Born in Jakarta on March 8, 1981 and has completed his Bachelor's degree in Electrical Engineering - Universitas Gadjah Mada, Yogyakarta in 2004. During college, his hobby was with the world of computers, especially the internet, so he had worked on a web-making project using ASP, PHP, MySql and Access. His first project was to create the Bulaksumur Pos website, a UGM student community media with ASP and Access, then Kick Off. Another project was to create the MLM Acintya.net website and continued with Ayudya.net and Javaart.net (handicraft sales site via the web). In addition, the author is also developing a web-based Clinic Information System.

In addition to being an employee of a private company engaged in manufacturing (Production Spv.), it does not dampen his interest in studying and developing knowledge about web programming. The author is also active in pouring his ideas into his blog in addition to developing a blog independently as well, visit  http://www.recosmic.dd.am .

Copyright / License:

  • All X-Code Magazine materials can be downloaded, read, modified and distributed freely for non-commercial purposes, provided that the author's attributes are not deleted or changed.
  • Copyright is in the hands of the author and X-Code Magazine by following the GPL (General Public License) license.

Post a Comment

Previous Next

نموذج الاتصال