I am going to begin by covering the creation of page names that are completely static looking from every angle. The reason I put a lot of time into this aspect of page layout is that search engines have always given static pages higher priority over any pages with variables on the end of URLs. Also... as my sites started out being static, I wanted a way to turn my sites into completely dynamic animals without sacrificing the PR
that had built up over time within the existing page names.
So firstly we need our servers to parse .htm and .html
files as PHP
files. In order to achieve this we simply add a mime type preferably to the httpd.conf file but failing that the .htaccess file is fine. You should always try to add any global variables that you would put in .htaccess into the servers httpd.conf master file. The reason for this is that the changes will become available on all your sites and you won't have to reiterate your code all over the place.
Also the httpd.conf file is loaded into memory on server boot while .htaccess files have to be parsed and checked in a hierarchical fashion creating a server performance reduction. The location of this file on my server is /etc/httpd/conf/httpd.conf and can be accessed using a SSH Shell tool. (Do a search online for putty.exe. It ROCKS!)
The line as stated earlier is:
Now we have to figure out a way to hide our variables within our page names, and how we can extract them as well.
To achieve this I start by using mod_rewrite to watch out for a certain string in an URL
that will only be located in pages I want it to affect. It is a common misconception (well it was for me) to think that mod_rewrite can only be used to hide variables as a directory structure. e.g http://somesite.com/blue-widgets.php?items=20&style=1
hidden and rewritten as http://somesite.com/blue-widgets/20/1/
The truth is that mod_rewrite being based upon REGEX (Regular Expressions) is pretty much unlimited in how you can use it. In my example I will tell mod_rewrite to look out for example- and when it finds it, to extract everything after example- but before .htm and turn it into a variable. Of course in the real world I would use a phrase that is very pertinent to the products I am selling.
lets use http://somesite.com/example-acme-green-shampoo.htm
I will use the following line of code in .htaccess
RewriteRule ^example-([^/]+).htm$ /example.htm?$1 [L]
Lets break this line down so we understand it. Rewrite rules basically have 3 parts.
The first part is the search phrase, starts with a ^ and ends with a $ sign. Inside that we tell mod_rewrite to look out for example-([^/]+).htm. The brackets tell mod_rewrite that we are creating a variable. If you had more than one pair of brackets each pair would become variables $1 $2 $3 etc. consecutively. Inside these brackets we have [^/]+ [^/] is REGEX code meaning any character. The + on the end means that the string can be any length and then we have .htm at the end followed by a closing $ sign. So mod_rewrite has been told to watch out for anything starting with example- and ending with .htm and to turn anything in the middle into a variable which becomes $1
The second part is the target page. In this case /example.php
?$1. So mod_rewrite sends the request to example.php
in the root directory and adds a ? mark then tacks the variable on the end giving us:
The third and final part of Mod_Rewrite is Either ([R] Rewrite the URL
) or ([L] Lock the URL
). As we used a [L] The URL
in the browser remains locked so even though the page is delivered, the browser URL
If we were to change it to an [R] (Great for trouble shooting!) the browser URL
This is the clearest explanation of mod_rewrite I am capable of.