
I had been getting a lot of questions lately about the mod_rewrite. And though there are a lot of tutorials out there to help you out, not many are easy to understand and not many teach the basics that one require. So I thought I would write a little article on how to use mod_rewrite the one module that has been a matter of thought for every new and a little experienced developers. Is it that difficult? We find out here.
So what actually is mod_rewrite?
It is a module which provides a powerful way to do URL manipulations. It lets you do all kind of URL manipulations for your site that helps you build pretty URL or Search Engine friendly URLs. And it just does not limit to that. This thing is complex while you are a beginner but the basics that you would need can be addressed by referring to some tutorials and here is one. In other words: With mod_rewrite you either shoot yourself in the foot the first time and never use it again or love it for the rest of your life because of its power.
To use mod_rewrite you first need it to be loaded on your server. You can check this by using phpinfo(). Assuming you have PHP installed, simply save <?php phpinfo(); ?> in a text file with .php extension and upload to your server. Run the script by pointing your browser to the address and search for “mod_rewrite”. You should find it under “Loaded Modules”. If you are using Online server than you can ask your host to enable it if it is not done yet. Though in most cases it is enabled before hand.
Now that you have checked whether your server has mod_rewrite enabled, now we see how the structure looks like. Well it is saved in a simple file called ‘.htaccess’ . Yes the name is just .htaccess no name and no any other extensions and it is saved in the root folder of your site eg: www or public_html . So all you have to do is open notepad write down a few line of codes and save it as .htaccess and upload to you root folder and you are done.
Some basic use of mod_rewrite are :
- Preventing Hot Linking
- Search Engine Friendly URLs /Pretty URL
- Redirects
- Prevent Hot Linking :
This is used to prevent other websites from using your pictures on to their site by directly linking it to your site, this can use a lot of your bandwidth. So you can do this by simply writing a few line of code using mod_rewrite.
[php]
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !domain.com
RewriteRule \.(gif|jpg|jpeg|png)$ nosteal.jpg [L]
[/php]All that the first line does is It starts the ReWrite Engine so we can do some magic ;) . Then we check if the http referrer is our site or not. If it is not then it transfers all requests made to files with .gif ,.jpg, .jpeg, and .png extension to ‘cheatingisbad.jpg’ . Don’t forget to change domain.com to your domain name.
- Search Engine Friendly / Pretty URL :
This might be a known term to you and many might be reading this article for this part. It is a known fact that search engines love a neat URL like http://domain.com/page/abc rather than http://domain.com/index.php?page=abc . The reason search engines love it is because they think any URL with ‘?’ sign is dynamic and it loads data dynamically so the page keeps changing, so to make believe search engines and also to make a easily remember able link mod_rewrite comes into action.
[php]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/$ page.php?name=$1
[/php]This converts http://domain.com/page.php?name=about_us to http://domain.com/about_us/
[php]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/$ page.php?name=$1
[/php] - 301 Redirects :
There are some times when your one page is ranking good on google or any other search engine and you do not want to loose all the traffic it gets but still you need to move your page from that location, what do you do? Loose traffic or take risk and stay at that location though it is not suitable? Well you keep the rankings too and change location also and all with the help of mod_rewrite. With this you can easily redirect a page that gets you traffic to a new page or location using a 301 permanent redirect. [php]
RewriteEngine ON
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
[/php]This piece of code transfers any of your request made to old domains like http://olddomain.com/abc.html will be redirected to http://newdomain.com/abc.html
- Ban IP Addresses :
[php]RewriteCond %{REMOTE_ADDR} 123.45.67.89
RewriteRule .* you-are-banned.html [R]
[/php]