Log In | Register | March 28, 2024

Share
 

.htaccess - September 18, 2010

Rewriting Dynamic URL’s to SEO URL’s Quick and Easy

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.

/index.php?page=contact

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:

/page/contact.html

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.

RewriteEngine On
RewriteBase /
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.

Download SEO URL’s Tutorial Files

Post By: | FavoriteLoadingAdd to favorites

4 Comments

kyleinsea
Wednesday, September 22, 2010

I’ve been working at executing this tutorial, but so far no luck. I’ve looked on line and I have found some similar tutorials as well, but no luck there either. Is there anything that needs to be set on the server for this to work? Any settings involving the .htpasswd file?

Thanks!

Frank Perez
Wednesday, September 22, 2010

Yes, on the server you need to make sure that you have Mod Rewrite enabled. You will not have to do anything with .htpasswd

vimeospot.com
Thursday, January 6, 2011

please look at my site and tell me what rewrite rule shall i use to convert into static pages.

Megapolis Hack
Thursday, September 25, 2014

Very good article! We are linking to this particularly great content on our website.
Keep up the good writing.

Leave a Comment



Need Help? Ask a Question

Ask anything you want from how to questions to debug. We're here to help.

You Must Be Logged In To Post A Question.

Log In or Register