There’s a long time in the galaxy since I haven’t post on my blog and now, finally here I am, posing a new article. Today I will speak about htaccess mod_rewrite. Well despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo.
The majority of the web servers, that are based on Apache, have the mod_rewrite enabled so you don’t have to get dirty to install it on the hosting server.But anyone of those that are developing web application based on Apache server, using PHP have a local installed server therefore mode_rewrite is also necessary in testing process.
Installing mod_rewrite :
- On Linux: edit the Apache configuration file ( httpd.conf ) and uncomment LoadModule rewrite_module modules/mod_rewrite.so (remove the pound ‘#’ sign from in front of the line) . Also be sure that the lines containing ClearModuleList and AddModule mod_rewrite.c are uncommented . Now you can create the famous .htacess file on the root of you website and start adding your code.
- On Windows: well as we are expecting, things are a bit more complicated because on Windows you cannot create a file that doesn’t have a name (.htacess is a file that only have an extension). For first just uncomment the same lines as in the Linux example. Now you have to tell Apache to use you’re custom file instead of .htacess file, so search in the configuration file (httpd.conf) for the following lines:
# AccessFileName: The name of the file to look for in each directory # for additional configuration directives. See also the AllowOverride
# directive.
#
AccessFileName .htaccess (change the .htaccess to [name].htaccess where name can be whatever what you wish )
You can escape this step and create the .htacess file even on windows, actually the restriction of creating a file without name is an restriction of Windows Explorer is not an restriction of the operating system . Open the command (cmd), go to the root folder of the website and type copy con .htacess and press Ctrl+Z. well… that’s all folks.
Maybe you are asking yourself about mod_rewrite and where to use it, well the short and simple answer is : for simplifying the URL of a website, simpler/shorter URL are easier to write easier to remember. For example instead of having an URL like this www.example.com?id_product=23123&color=23 you can have something like www.example.com/iphone-white. Mod_rewrite basicly make use of regular expression to rewrite an specified URL.
When to use it: mod_rewrite can be used in many circumstances where you may want to rewrite the an URL, also you can create dynamically virtual hosting entry; practically mode_rewrite is an “Swiss Army Knife”.
When not to use it: well as I have mentioned before mode_rewrite make use of regular expression, and evaluating regular expression takes time, use memory and the global performance of the server is decreasing.
I will provide you some basic example of using mode_rewrite. .htaccess file should begin with:
Options +FollowSymLinks RewriteEngine On RewriteBase /
- Redirecting
Redirect /index.php http://www.example.com/ -this example redirect the current site to www.example.com
RedirectMatch (.*)\.gif http://images.example.com$1.png -In this example, we’ve taken all of our GIF files, converted them to PNG files, and
moved them to another server - Require www
Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] - Denying access to a folder except of fopen file from php
RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/.*\ HTTP [NC] RewriteRule .* - [F,L] - Limit access during some hours
Options +FollowSymLinks RewriteEngine On RewriteBase / # If the hour is 7 RewriteCond %{TIME_HOUR} ^7$ RewriteRule ^.*$ - [F,L]
These samples are here just to open your appetite for mode_rewrite. Practically this tool can be used in any other ways,yes… the limit is your imagination.
The limit is indeed our imagination, but some take it too serious. Got this client who has a script which for every possible link in the website is defined in the bloody .htaccess file, I get sick everytime I see one.
Good article Andrei! Congratulation! As far as i go , mod_rewrite has always been something very complicated , but after reading this article I see it’s not that hard to create nice url’s.
PS: Keep the articles coming … you write so rare!!!!
@tomitzel … yes when you own a hammer all around you looks like a nail, but let’s admit mode_rewrite is pretty cool.
@Eugen … the articles are coming, I think
.