The hurdle you must overcome when implementing the URLs shown earlier is that they don and #39;t actually exist anywhere in your web site and #46; Your site still contains a script and #45; named and #44; say and #44; product and #46;php and #45; which
expects to receive parameters through the query string and generate content depending on those parameters and #46;URL rewriting allows you to transform the URL of such an incoming request to a different URL according
to a defined set of rules and #46; You could use URL rewriting to transform the previous non and #45;existent URLs to product and #46;php and #63;product and #95;id and #61;123 and #44; which does exist and #46;
The URL rewriting service is provided by the Apache web server through the mod and #95;rewrite module and #46; PHP does not have anything to do with it and #44; because the PHP engine takes charge only once the and #46;php file is executed and #46;
The mod and #95;rewrite module is the de and #45;facto standard for URL rewriting in the Apache world and #44; and is typically supported by any Apache and #45;based hosting package and #46;The mod and #95;rewrite Apache module is an invaluable tool to web developers tasked with architecting
complex dynamic sites that are still search engine friendly and #46; It allows the programmer to declare a set of rules that are applied by Apache on and #45;the and #45;fly to map static URLs requested by the visitor to dynamic
query strings sent to various PHP scripts and #46; As far as a search engine spider is concerned and #44; the URLs are static and #46;
Installing mod and #95;rewrite
If you and #39;re implementing the exercises in this book using an external hosting provider and #44; it and #39;s very likely that mod and #95;rewrite is already installed and enabled and #46; In that case and #44; you can simply skip to the next section and #44; and #34;Testing
mod and #95;rewrite and #46; and #34; Consult your web host for information regarding whether mod and #95;rewrite is supported and enabled and #46; If you and #39;ve installed Apache yourself and #44; read on and #46; Because of its popularity and #44; mod and #95;rewrite is now included
with all common Apache distributions and #46; If desired and #44; you can verify if your Apache installation has the mod and #95;rewrite module by looking for a file named mod and #95;rewrite and #46;so under the modules folder in your
Apache installation directory and #46;
Testing mod and #95;rewrite
Once mod and #95;rewrite is installed and enabled and #44; you add the rewriting rules to the Apache configuration file and #44; httpd and #46;conf and #46; Apache also lets you save configuration options and #40;including rewriting rules and #41; on a
per and #45;directory basis to a configuration file named and #46;htaccess and #46; All you have to do is create a file named and #46;htaccess into a directory of your application and #44; and Apache will read it automatically when accessing
that directory and #46;As with mod and #95;rewrite and #44; and #46;htaccess isn and #39;t related to PHP in any way and #46; The and #46;htaccess file is strictly an Apache configuration file and #46; You and #39;ll see how it works in the exercise that follows and #46;
Using and #46;htaccess is useful in the following scenarios and #58; You don and #39;t have access to the global configuration file and #44; httpd and #46;conf You want to have customized configuration options for a particular directory of the application and #46;
You want to be able to change the configuration options without restarting Apache and #46; You are required to restart Apache after modifying httpd and #46;conf and #44; and this is problematic if you have
live applications running on the web server and #46;All of the exercises in this book are designed to work with the and #46;htaccess file and #46; This is the method we generally recommend for defining rewriting rules and #44; especially in the development stage when the rules
change frequently and #46; This way you avoid restarting Apache every time you change a rewriting rule and #46; It is also the only solution in a shared hosting environment and #44;
Rewriting Keyword and #45;Rich URLs
These URLs include keywords and #46; However and #44; we and #39;ve sneaked IDs in these URLs and #44; in a way that isn and #39;t unpleasant to the human eye and #44; and doesn and #39;t distract attention from the keywords that matter and #44; either and #46;
Building a Link Factory
Back in the days when you worked only with dynamic URLs and #44; it was easy to build the URLs right in the application code without much planning and #46; You cannot do that here and #46; If you want to use keyword and #45;rich
URLs in your web site and #44; just having a RewriteRule in your and #46;htaccess file isn and #39;t enough and #33; You also need to ensure that all the links in your web site use these keyword and #45;rich versions consistently throughout the
web site and #46; Obviously and #44; including the URLs manually in the web site is not an option and #45; it and #39;s a dynamic site after all and #44; and the link management task would quickly become impossible to handle when you have a
large number of products in your catalog and #33; Fortunately and #44; there and #39;s a very straightforward solution to this problem and #44; which as soon as it and #39;s put in place and #44;takes away any additional link management effort and #46; The solution we and #39;re proposing is to use a function to
generate the new URLs based on data already existing in your database and #44; such as product or category names and #46;