Search Engine Friendly URLs with PHP

Nov 11, 2008 | Tags: SEO, PHP | del.icio.us del.icio.us | digg Digg

3. Using Apache's mod_rewrite

All incoming requests, except those for static resources, need to go to a single index.php file. This script is the central point to display your articles. It retrieves the filename being requested, get the article that matches from the database, and then display the article. Note that if you already use index.php for your homepage, move it home.php or any other file you like.

We route all requests to index.php by using Apache's mod_rewrite. Put the lines below in a new file, name it .htaccess and place it in the webroot directory.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

The RewriteRule is very simple and can be interpreted as "for any url, except those for static resources, use index.php instead". With this configuration, requests for static pages like:

http://yourdomain.com/home.php
http://yourcomain.com/contact.php

still go to home.php and contact.php respectively, while other requests like:

http://yourdomain.com/Decline_of_The_Roman_Empire
http://yourdomain.com/final-war-of-the-roman-republic.html
http://yourdomain.com/Richard-1-of-England

will go to index.php instead.

In this page

  1. Introduction
  2. Give Each Article a Filename
  3. Using Apache's mod_rewrite
  4. The Wrapper Script
  5. Results
  6. Conclusion
Leave a comment

Tags

Recent Posts

  1. OpenCV Utility: Reading Image Pixels Value
  2. OpenCV Circular ROI
  3. OpenCV 2.0 Installation on Windows XP and Visual Studio 2008
  4. Runtime ROI Selection using Mouse
  5. Real Time Eye Tracking and Blink Detection
View Archives

About the Author

avatar Cool PHP programmer writing cool PHP scripts. Feel free to contact
Tel. +62 31 8662872
+62 856 338 6017
ICQ 489571630
Skype dede_bl4ckheart
Yahoo dede_bl4ckheart
Google nashruddin.amin

Recommended Sites:

Hacker's HTTP Client
HTML and CSS Tutorials
Stop Dreaming Start Action
Online Quran and Translation