In this tutorial we are going to go over the steps that will allow you to convert your dynamic URL’s to nice SEO static links. Lets start by examining the URL we are going to be converting.
This is a pretty typical looking dynamic URL. Notice we are assigning the identifier of contact to the page key. The only part in this URL that will change is the identifier contact.
Lets say we want to convert that URL to something more like this:
Notice, we are no longer referencing the index.php file, and we are using the identifier before the .html extension.
*Note: After you convert your URL’s to the SEO friendly style, you will still be able to use any GET method to get the parts of your URL. The way these rewrites work are similar to a mask. Both URL’s in turn will work.
To accomplish the above result is very simple. First we will need to create a .htaccess file in our root directory. After creating the file open it up, and insert the following code.
RewriteRule ^page/(.*)\.html /index.php?page=$1 [L,PT]
Now lets break it down. The first line we need to insert is the RewriteEngine On. By default some servers will not have Mod Rewrite enabled. Adding this line to your htaccess will enable URL Rewriting for your site.
Second we should start by adding the line RewriteBase / which will set the base url for your site.
Finally we add the RewriteRule. You will see a few parts that we’ll need to go over. For RewriteRule to work we will need to always start each rewrite with the directive RewriteRule. After that you need a space and the ^ character. The ^ character signifies the start of a string. After that we added the key term page followed by a forward slash. After the forward slash we added the regular expression (.*). The dot in the regular expression stands for any single character. The asterisks stands for more then one. Together (.*) allows the htaccess to prepare for any number of characters to be passed through. We followed this with a backslash which is used as an escape character for the dot. Then we append html. This portion of the RewriteRule sets up the SEO URL.
Now that we have the SEO portion setup, we need to append the dynamic URL so that the .htaccess can mask it to work as the new SEO URL’s. So now lets append our /index.php?page=$1
You will see that instead of the indentifier, I used a variable of $1. Remember earlier we created that 1 regular expression. Creating the variable will tell your .htaccess to replace the regular expression with your variable. Depending on how many regular expressions you set, you will have to use variables with integer increments of 1, 2, 3, etc.
After adding the dynamic portion to it we should append the following flags [L,PT]. Adding this flag will ensure that your cookies will work fine throughout the domain.
Attached is a sample file and htaccess that I created. Let me know if you have any questions below.