More .htaccess mod rewrite examples

From DreamHost

Jump to: navigation, search

Contents

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 “-” for word separation, 0 or more times. If you want it to support upper case too, use “([a-zA-Z-]*)
[QSA,L] 
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

Notes

  1. You can use the ampersand character in the target redirect however without some special running gear you can not use an ampersand in the source URL. Definitely inconvenient!
  2. The arguments in the static URL are separated by a character of your choosing that you do not use in any of your query string arguments. Otherwise you will confuse the situation when it does the separation of fields.

Direct translation does not work. You might be tempted at this point to turn a dynamic URL into a static URL by this method:

index.cgi?var1=fudge&var2=crap becomes index-var1=fudge&var2=crap.html

If this would work you could very simply change all of the dynamic URL of your site and turn them into static URL. Then by using mod_rewrite you could turn them back into the dynamic URL most easily digested by your script. However unfortunately after some experimenting mod_rewrite likes to eat the ampersand characters and this will not work.

Block Access to a Directory

If you have a directory named blah 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 directories:

RewriteRule (^|/)\.svn(/|$) - [F]
(^|/) 
Matches either the start of the path or a “/”.
(/|$) 
Matches either the end of the path or a “/”.
[F] 
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) or some where in the middle (http://example.com/my/path/blah/stuff/sub/dirs) or at the end of the URL (http://example.com/my/path/blah).

Personal tools