More .htaccess mod rewrite examples

Being SEO Friendly
I am making a PHP site and want it to be SEO friendly also. What I want is:


 *  http://www.mysite/1.html  to redirect to  http://www.mysite/abc.php?id=1 


 *  http://www.mysite/place/name.html  to redirect to  http://www.mysite/place/abc.php?id=name 

Method
Options +FollowSymLinks RewriteEngine On RewriteRule ^([0-9]+).html /abc.php?id=$1 [QSA,L] RewriteRule ^place/([a-z-]*).html /place/abc.php?id=$1 [QSA,L]


 * ([0-9]+) : allows any digit, and only any digit, 1 or more times.
 * ([a-z-]*) : allows any lower case letter, plus &ldquo;-&rdquo; for word separation, 0 or more times. If you want it to support upper case too, use &ldquo;([a-zA-Z-]*)&rdquo;
 * [QSA,L]</tt> : appends this to your internal scripting query string, and makes it the Last rewrite rule executed.

Observations

 * 1) After using this method you can retrieve the webpage with either address type. This is handy for retro-fitting a website that was not designed with mod_rewrite in mind.  This is good because it does not blow any bookmarks saved on users computers. In addition if you slip and miss anything the old way still works albeit not in a manner as friendly to search engines. This was tested with the above method on DH.
 * 2) You can use regular expressions in your .htaccess file.
 * 3) You can test this method out using a subdirectory as .htaccess appears to be able to be done that way. Above abstacted method was tested on a DH shared server on a subdirectory.
 * 4) Another language to learn: QSA=Query String Append (??) what L is short for? L=Last rule
 * 5) Regular expressions under this (apache? unix?) appears to be slightly different than PERL as the period character (.) does not appear to require the slosh escape character.

Download the mod_rewrite cheat-sheet for basic formatting, syntax & examples in PDF format or PNG format.

Wild Cards
Another convenient item to know is the wild card operator in the regular expression. An example of usage is:

Options +FollowSymLinks RewriteEngine On RewriteRule ^index_(.*).html /TestURL/index.cgi?mode=$1 [QSA,L]

More examples
Yet another take on it allows all your mapping to be done with minimal alteration of a PERL script .htaccess file.

Options +FollowSymLinks RewriteEngine On RewriteRule ^index-(.*)-(.*).html /TestURL/index.cgi?fudge=$1&crap=$2

Block Access to a Directory
If you have a directory named blah</tt> that you want to block, but it can occur anywhere in your directory tree, use the following:

RewriteRule (^|/)blah(/|$) - [F]

I used this specifically to block access to .svn</tt> directories:

RewriteRule (^|/)\.svn(/|$) - [F]


 * (^|/)</tt> : Matches either the start of the path or a &ldquo;/</tt>&rdquo;.
 * (/|$)</tt> : Matches either the end of the path or a &ldquo;/</tt>&rdquo;.
 * [F]</tt> : Flags the URL as forbidden, sending a HTTP 403 response.

The two expressions in parenthesis are required because the directory can occur at the root URL ( http://example.com/blah/sub/dirs </tt>) or some where in the middle ( http://example.com/my/path/blah/stuff/sub/dirs </tt>) or at the end of the URL ( http://example.com/my/path/blah </tt>).