Create SEO Friendly URLs With Htaccess Mod Rewrite in 3 Easy Steps

Posted in SEO , Web Development on 18 November 2009 55 comments

What is SEO Friendly URL and Why?

An example of SEO friendly URL can be seen in this page. Compare it with urls like http://mysite.com/?t=34 , you will find it easier to remember and the it also clearly tells you what this page is about. Furthermore, the words in the URL might match the search keywords, bringing more traffic from search engines.

Make SEO friendly URL yourself

I don’t have to worry about SEO friendly URL in WordPress since its already built-in. However, I need it for my premium wordpress themes website, which is not based on WordPress. So I started to look into this matter.

Using htaccess and mod rewrite, you can make SEO friendly yourself in 3 easy steps.

1. Creating .htaccess file

Open NotePad (yes, the windows notepad) > File > Save As > Change the “Save as Type” to “All Files” > enter “.htaccess” as the name and press Save. You’ve created a htacces file.

Now paste this codes below into it.

Options +FollowSymLinks
 
RewriteEngine On

The first line Options +FollowSymLinks is required for some server configurations.

2. Create your own rewrite rule

Example 1
For example, if you want to change links like http://mysite.com/index.php?topic=rules to http://mysite.com/topic/rules/, here’s the rewrite rule.

?View Code HTACCESS
Options +FollowSymLinks
 
RewriteEngine On
RewriteRule ^topic/([a-zA-Z0-9]+)/$ index.php?topic=$1

• Like regular expressions, the [a-zA-Z0-9] matches lower and uppercase of alphabets and numbers.
• The asterisk inside the brackets + is a quantifier that match 1 occurence to infinite occurences.
• Combining them, ([a-zA-Z0-9]+) matches alphanumerics of at least 1 character.
• The caret ^ means “start with”, meaning the URL starts with the word “topic”.
• The dollar sign $ means “end”, meaning the URL ends with a slash.
• The $1 is backreference, it carries the content of the first group of brackets.

In other words, when user enters http://mysite.com/topic/faqs/ , the page being called and run would be http://mysite.com/index.php?topic=faqs

Example 2
If you want to change URLs like http://mysite.com/index.php?product=productname&price=30 to http://mysite.com/products/productname/30/. Basically its similar to above.

?View Code HTACCESS
Options +FollowSymLinks
 
RewriteEngine On
RewriteRule ^products/([a-zA-Z]+)/([0-9]+)/$ index.php?product=$1&price=$2

• The [0-9] in matches numbers only.
• The plus sign is a quantifier that match 1 or more occurences.
• Combining them, ([0-9]+) means 1 or more numbers.
• Similarly, $1 will be the first brackets : product name and $2 would be the second brackets : price.

Example 3
If you want to change URLs like http://mysite.com/article.php?id=45 to http://mysite.com/article-45.html, here’s how:

?View Code HTACCESS
Options +FollowSymLinks
 
RewriteEngine On
RewriteRule ^article-([0-9]+)\.html$ article.php?id=$1

• The new thing here is the \. (backslash followed by a dot).
• The backslash here “escapes” the dot, so that the dot means a real dot instead of “anything”.

3. Extra Stuff

Custom 404 error page

Put this in your htaccess if you would like to have a custom 404 error page instead of the default one.

?View Code HTACCESS
ErrorDocument 404 /404.php

Change the 404.php to your page.

Disable directory browsing

For security purpose, its best to disable directory browsing so that people won’t know what files you have. Use this :

?View Code HTACCESS
Options All -Indexes
Protect .htaccess files

This should disallow other to access your .htaccess file, just like disallowing others to access your wordpress’s wp-config.php

?View Code HTACCESS
<files .htaccess>
order allow,deny
deny from all
</files>

Others references you might need

Mod Rewrite Syntax

 

Posted by Zen on 18 November 2009 • 213,092 visits 55 comments
Tags : ,


or Subscribe to specific category only :




  - 55 Comments


Mark says:

Nice tips about .htaccess :)
Thanks bro :)

adrian says:

please, can you tell me how to remove the special character “%252d” from the url: http://www.mydomain.ro/products/NONI%252dSirop-Concentrat-din-Fruct-Noni.html, using htaccess ?

Arnie says:

http://192.168.1.10/phptest/add.php?stdid=10

above one is my url and i wanna rewrite it like

http://192.168.1.10/phptest/add/student10

i coded it like below
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^192.168.1.10$ [NC]
RewriteRule ^add-([0-9]+)\.php$ phptest/add.php?stdid=$1

Where Am I Mistaken.. Help. ThankxxXx

Arnie says:

i’m getting problems. my URL is like
http://192.168.1.10/phptest/add.php?stdid=11

and i wanna rewrite it like
http://192.168.1.10/phptest/add/student11

and my code is like
RewriteEngine On
RewriteCond %{HTTP_HOST} !^192.168.1.10$ [NC]
RewriteRule ^add-([0-9]+)\.php$ add.php?stdid=$1

what is wrong in this code??? i’m not getting it. plz help, will my pleasure. Thanks :)

MSH says:

I am trying to rewrite my website urls to make them more SEO friendly, but I still don’t get it….

So let’s say for example, I want to rewrite this url using your example 2 – http://www.mysticscenthills.com/index.php?main_page=product_info&cPath=0&products_id=23283&zenid=7d9af528805ee168a685f87727240147

I created an .htaccess. notepad, I copy this link and then what?

What are the next few ALL steps for me to incorporate this to my website in order for it to work? I’m confused…..

Example 2
If you want to change URLs like http://mysite.com/index.php?product=productname&price=30 to http://mysite.com/products/productname/30/. Basically its similar to above.

?[Copy to clipboard]View Code HTACCESSOptions +FollowSymLinks

RewriteEngine On
RewriteRule ^products/([a-zA-Z]+)/([0-9]+)/$ index.php?product=$1&price=$2
• The [0-9] in matches numbers only.
• The plus sign is a quantifier that match 1 or more occurences.
• Combining them, ([0-9]+) means 1 or more numbers.
• Similarly, $1 will be the first brackets : product name and $2 would be the second brackets : price.

Fashion says:

This is really helpful. Thanks for sharing.

Mazhar says:

Really nice and helpful post. just re wrote my .htaccess file with the tips given. Keep upto good work..

cross says:

hello sir
by this post now m able to change my site url.
previously it was
http://www.xyz.com/product.php?category=5&product=6
now it coming like
http://www.xyz.com/product/category/5/product/6
but in product.php page i need the 2 ids i.e. category id and product id to display the product and now the ids are not coming. please give some solution to this.

can i change my url as product//.html.

Thanks

tusher1001 says:

This is really an owesome post.I like this very much.It is very helpful near to me.

tusher1001 says:

The post of this website is highly informative.I like it.

native clams says:

Really helpful site. Go to my site for SEO tips.

hamid says:

hi

Thanks for sharing.

amal says:

very nice and very useful article thank you

shoukhin says:

One of most helpful post ever found. Thank you

tusher says:

it is very nice site and i like this very much.

Trackbacks

  1. Step 2: SEO Friendly URL | Siddharth Shah | Sid | Siddhath SEO Tips | Sid photography
  2. .htaccess REWRITE RULES | .htaccess REWRITE RULES - .htaccess REWRITE RULES - .htaccess REWRITE RULES = .htaccess REWRITE RULES | NURSING CARE
  3. how can i remove .html from url? - Bizzteams - Page 2

Leave a Reply

You must be logged in to post a comment.

Previous Post
«
Next Post
»