mod_rewrite and url writing
In situations when a clean, easy to read url cannot be provided, such as those generated dynamically from a database, it can be crucial to emulate those urls for the benefit of visitors and spiders alike. Human visitors may quickly become confused by a url which contains combinations of characters, numbers, and even punctuation marks. Search engines, likewise, will often balk at punctuation and other special characters in a url, particularly the ampersand (&). Fortunately, there’s a method available for Apache file servers that can be used to mask these strange filenames into urls that look a lot more like a “regular” filename. The installation is called mod_rewrite, and once it has been installed on the server (most ISPs do use it), you can redirect how a filename is shown to the user through your .htaccess file.
Not only can a dynamically generated url be hard to remember, and trip up a search engine, they can also create a security issue. Potential hackers could decipher the correct data to send in a query string to penetrate the sites security. All things considered, there’s no reason not to make the url appear to be a normal-looking filename. And let’s not forget that you are going to want other human beings to link to your site. Make it easy for them to do so, and you’ll find they are more willing to take the time to enter your link into their own pages.
Let’s look at how we can change the following url in to the one that is printed afterwards:
http://www.mydomain.com/view.asp?category=shirts&prodID=42
http://www.mydomain.com/catalog/shirts/42/
Load .htaccess into a text editor. And since not all ISPs have mod_rewrite enabled, we’ll make sure it’s turned on for the directory by adding the following line as the first line of .htaccess:
RewriteEngine on
RewriteRule ^catalog/([0-9]+)$ /catalog/$1/ [R]
RewriteRule ^catalog/([0-9]+)/$ /view.asp?prodID=$1
These three lines, save the .htaccess file, and you’re done. The first line enables mod_rewrite, and needs no further discussion. The second line check to see if the “/” is appended to the end of the url typed by the users, and appends it if not. If the forward slash must be appended, the “[R]” is an automatic redirect back to the server with the filename corrected, and forces .htaccess to be processed again for the next modifier. Note that there are other ranges than [0-9]. You can also use [A-Z], [a-z], or a combination of [A-Za-z]. The “+” allows us to evaluate the whole number, regardless of the number of digits used (ie, 1 or 176234), and the number in parenthesis “()” is so that we can back reference it to produce the value of $1.
There are a few other modifiers that can be used on the rule, but the basic 3 lines given here will suffice for most situations. Consult the Apache website for a full list of the modifiers that may be used, as well as other commands that may be placed in the .htaccess file. Error redirection to load moved pages, or handle missing (error 404) ones may be accomplished with the mod_rewrite command also, as well as through other .htaccess methods.
Article written by SEOnotepad.com

No Comment Received
Sorry the comment area are closed for non registered users