|
Home ⇒ About Me ⇒ Resume ⇒ Knowledge ⇒ Web Hosting ⇒ Apache ⇒ Mod_rewrite ⇒ Mod_rewrite
Rewriting urls using Apache's Mod_rewriteI learnt how to use mod_rewrite in an early search engine project that I did. The abilitiy to make dynamic urls appear static adds a great amount of power and simplicity to site design. For example being able to make large amounts of dynamic pages from a DB appear static, greatly adds to the search engine exposure of any given website, and allows the author to much more effectively 'write urls', to either add in search engine optimization efforts or to give functionality to a user. A simple example of this is a community website that is created an allows users to have online porfolios. Without mod_rewrite we might have to have an url look like this to get to the users profile: http://www.somedomain.com/profile.php?username=someuser simple use of Mod_rewrite allows us to convert the above url into: http://www.somedomain.com/someuser The mod rewrite used to perform the above would look like: RewriteRule ^([^/]+)/? profile.php?uid=$1I have used mod_rewrite on ryanfyfe.com. To see an example, note that all of these links are the exact same page, but can be made to look alot different from one another:
Mod_rewrite can also be used on images, to both provide a different and dynamic url to a static image. It can also be used to disable remote hotlinking of images. Below is the exact .htaccess file used on this website. ryanfyfe.com's .htaccess file
ErrorDocument 404 http://www.ryanfyfe.com/
Options +FollowSymlinks
Options All -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
RewriteRule ^home_1.html / [L]
RewriteRule ^([^/]+)_([^/]*).html pages.php?q=$1&id=$2 [L]
RewriteRule ^([^/]*).html pages.php?id=$1 [L]
RewriteRule ^([^/]+)/? pages.php?id=$1 [L]
|