URL rewriting-various exercises-seo

 Installing mod_rewrite
 Testing mod_rewrite
 Working with regular expressions
 Rewriting numeric URLs with two parameters
 Rewriting keyword-rich URLs
 Building a link factory
 Pagination and URL rewriting
 Rewriting images and streams

If you’ve installed Apache yourself, read on. Because of its popularity, mod_rewrite is now included
with all common Apache distributions. If desired, you can verify if your Apache installation has the
mod_rewrite module by looking for a file named mod_rewrite.sounder the modulesfolder in your
Apache installation directory.
However, mod_rewrite may not be enabled by default in your Apache configuration. To make sure,
open the Apache configuration file, named httpd.conf.

Once mod_rewrite is installed and enabled, you add the rewriting rules to the Apache configuration
file, httpd.conf.

You use this to have mod_rewrite translate my-super-product.htmlto product.php?product_id=123.
The linethat precedes the RewriteRuleline is a comment.
# Translate my-super.product.html to /product.php?product_id=123
RewriteRule ^my-super-product\.html$ /product.php?product_id=123
You can find the official documentation for RewriteRuleat http://www.apacheref.com/
ref/mod_rewrite/RewriteRule.html.
In its basic form, RewriteRuletakes two parameters. The first parameter describesthe original URL that
needs to be rewritten, and the second specifies what it should be rewritten to. The pattern that describes the
original URL is delimited by ^and $, which assert that the string has nothing before or after the matching
text (explained further in the following sections), and its contents are written using regular expressions,
which you learn about next.

Using RewriteBase

The regular expressions and scripts in this book assume that your application runs in
the root folder of their domain. This is the typical scenario. If, however, you host your
application in a subfolder of your domain, such as
http://www.example.com/seophp, you’d need to make a few changes to accommodate 
the new environment.

The most important change would be to use the RewriteBasedirective of mod_rewrite
to specify the new location to act as a root of your rewriting rules. This directive is
explained at http://www.apacheref.com/ref/mod_rewrite/RewriteBase.html.
Also, the rewritten URL should lose its leading slash, because you’re not rewriting to
root any more. Basically, if you host your first example in a subfolder named seophp,
your .htaccessfile for the previous exercise should look like this:

RewriteEngine On
RewriteBase /seophp
RewriteRule ^my-super-product\.html$ product.php?product_id=123

RewriteRule command and its parameters must be written on a single line in
your .htaccessfile. If you split it in two lines as printed in the book, you’ll get a 500 error from the
web server when trying to load scripts from that folder.