Mastering Apache's mod_rewrite
mod_rewrite For Ultimate URL Control
The Apache web server has a number of very useful and powerful features. One of these is the ability to rewrite URLs. URL rewriting allows you to create short or fancy looking URLs transparently, without the user's knowledge. This feature is particularly useful for search engine optimization. Apache uses the module mod_rewrite to enable the URL rewriting feature. Let's look at how to enable, configure, and tweak mod_rewrite in your Apache installation.
In this tutorial, I'm using a Red Hat Linux machine with Apache 2.x setup and working. These instructions should work just fine on other flavors of Linux but may require some minor changes with the paths of files mainly. Most Linux
Other Stories on LinuxPlanet
LoadModule rewrite_module modules/mod_rewrite.so
You can try searching for the word mod_rewrite in httpd.conf. If you still don't find the line it is quite likely that mod_rewrite is installed on your machine but has not been enabled. Look for the file mod_rewrite.so under your Apache installation. Once you find the file, all you have to do is add the following line in your Apache configuration file /etc/httpd/conf/httpd.conf in the section where all the other LoadModule settings are:
LoadModule rewrite_module modules/mod_rewrite.so
Restart Apache to have the new settings kick in. Now let's move on to the fun part - configuring URL rewriting.
Before you get your hands dirty here's the basic syntax used to create URL rewriting rules for mod_rewrite.
RewriteRule rewrite_source rewrite_destination [optional_flags]
There are four parts to the rule configuration. You first invoke RewriteRule, which is the static syntax to create a rule. Then you enter the source URL, which is the address that will be typed by the user in her web browser. After this comes destination URL, which is the page the web server will actually activate. Last is the optional flags section, which is where you can define the nature of the URL forwarding. With this relatively simple combination of entries, you can weave some wonderful magic with your URLs.
Let's begin by setting up a simple page forwarding. This is for a scenario where you have moved a webpage to a new location. Create a file home.html and enter the following line into it.
Hi I'm the First page
Access the page home.html in your browser. You should see the text you entered above. Now create another file, newhome.html, and enter the text Hi I'm the New Homepage into it and save it. Access it, and you should see the text you just entered. We're all set to setup our URL rewriting. We will set it up so that when a user comes to the page http://www.example.com/home.html she will be automatically taken to the new version of the page, which is at http://www.example.com/newhome.html. Note that, the URL displayed in the web browser will not change in this case.
Create a file with the name .htaccess in the same directory as the two HTML files you just created. Note the dot in the name of the file. Enter the following two lines in the file:
RewriteRule home.html newhome.html
Save the file and now access the page home.html in your web browser once again. If all went well you should see the text Hi I'm the New Homepage in the browser window. In the first line, we switched the mod_rewrite engine on. Then we set the URL rewriting rule to setup the forwarding using the RewriteRule option. This is where we use regular expressions and direct mod_rewrite to forward a user that lands on the old home page, home.html, to the new one, newhome.html. This was a very simple example, but it should give you a fair idea of how URL rewriting works in Apache and it will also let you know if your Apache installation has mod_rewrite installed correctly.
In the example above, we forwarded one page to another. The syntax was clean and simple. However, there are scenarios wherein it might fail to work, and what is even scarier is that it activates the rule in cases in which you might not want it to. For example, try entering the URL http://www.example.com/randomcharshome.html. Your URL rewriting setup will forward you to newhome.html, which is quite likely not the desired result. This is where regular expressions come to the rescue of mod_rewrite. Using regular expressions, you can make sure that you only apply the defined URL rewriting rules to the URLs that you want to. The example above can be modified to look something like this to take care of the issue demonstrated earlier and other such issues:
RewriteRule ^home\.html$ newhome.html
For readers not familiar with regular expressions, the ^ before home marks the beginning of a string, while the $ at the end of the line marks the end of the string. The \ before the .html is to explicitly tell mod_rewrite to treat the dot as literal character as opposed to a special character. Good knowledge of regular expressions, or regex as it is more commonly known, can be extremely helpful if you plan to use mod_rewrite to do more complex stuff. A good resource for this that I keep bookmarked is this cheat sheet.Article courtesy of Webreference.com
- 1Linux Top 3: Fedora 24, Peppermint 7 and Solus 1.2
- 2Linux Top 3: Alpine Linux 3.4, deepin 15.2 and Linux Lite 3.0
- 3Linux 4.7 Set to Boost Live Patching, Security and Power Management
- 4Linux 4.6 Charred Weasel adds USB 3.1 Support
- 5Linux Top 3: OpenIndiana 2016.04, Ubuntu 16.04 and Debian's New Leader