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

Posted in SEO , Web Development on 18 November 2009 29 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   @   18 November 2009 29 comments
Tags : ,


or Subscribe to specific category only :




  - 29 Comments


i can imagine it hurts for some people when they find out a long time after they made their URL that their URL is not keyword friendly. After you have built many bcklinks, gotten your reputation, and you have a long lasting domain age.

Doreen says:

Hi Thank u for the Tipps. What is the right Backend-Setting in Permalinks of WordPress.

Allgemeine Optionen: only in german here?
Standard
Tag und Name
Monat und Name
Numerisch
Benutzerdefinierte Struktur

zen says:

For wordpress, I use “Custom Structure” : /%postname%/

Cheers :)

Gary says:

Very helpful post. It has been very difficult for me to find anything good on this topic. thanks.

mark says:

thanks, that was a nice post to go through, I look forward to reading more of your stuff

sovanndy says:

Could you explain about (.*?)?

what does it mean?

I don’t understand this rule

car freak says:

great tips,,thanks a lot

Tanya says:

Ey, what about phpbb3? it is possible do something like that in forums? i found all the time mods like Phpbb seo, but it means a lot of changings the i don’t wanna do.

thanks for the tips!!!

Aurora says:

I too have that problem. This I tried and it only shows on the tab not on the web address in phpbb 3.07_pl. I tried the “easy mod” and that just gave me a blank page and it was NOT easy, so I had to reinstall my board. I hope they find a good solution soon. :)

Balaji J H says:

Thanks a lot
You saved me others worked but redirects to the real url
Only yours does the work

Thanks

Nayan Paul says:

Thanks Sir , Very Handy and useful post..

I have some query about seo friendly url,please help me

My demo site URL looks like
http://www.mydomain.com/index.php?module=listing&action=view&page=1
And Now loooks URL in that format
http://www.mydomain.com/index.php/module/listing/action/view/page/1

How i do that thru htaccess ? please Help Me , Thanks Again….

Ranjan says:

Hey, anybody can help me. I couldn’t able to do url rewriting.
here is my code of “.htaccess” file,

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^name/(.*)/$ rewrite.php?name=$1

Jyoti says:

Hi, Ranjan, Sory i couldn’t get u…
anybody can help him? thanks in advance.

Hi Ranjan,

If your url is http://www.website.com/rewrite.php?name=ranjan

and if you want it to be like http://www.website.com/name/ranjan/

Use this

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^name/(.*)/$ rewrite.php?name=$1

And make sure your rewrite.php file is written accordingly.

thanks for your great and value share, actually i’ve been searching for a couple days for it

i’ve find your blog appear on google, think you have good Seo technic, great job anyway.

Thanks for the easy to follow guide. I needed to create the SEO url’s on my RealEstateAnalysisFREE.com website for a long time, but always had troubles to figure it out. Now I just copied your code, change it a bit and it works like a charm! Thanks.

Needa says:

awesome its helpful :D

Helloo says:

Thank’s

its help my URL be search engine friendly :)

http://www.auctionsbuy.com

u gave the information in simple words. ^ key should always be used if it is the start of string which is to be matched, otherwise it will be matching URL names from in-between also, which may create trouble.

I tried the follwoing with no sucess:

RewriteRule ^directory/(.*)/$ index.php?q=$1 [L]

SFL: http://example.com/top-directory/directory/123

output to my script: http://example.com/top-directory/index.php?q=123

Can somebody tell me what am i doing wrong?

zen says:

You missed a slash after “123″

great post with good information. Is it possible to make http://anisbd.com/ebooks to browse like a subdomain http://ebooks.anisbd.com with .htaccess ?if yes how do i code it ?bro

chand says:

hi,

i have tried whatever show in this post. but my url is notwoking. see the url and suggesst me how to modify
if i am clicking on any of the menu it is coming like http://www.stage100floors.com/100bestbuy/search.php?categoryId=24.
but in need something like
http://www.stage100floors.com/100bestbuy/products/

my code is like
RewriteRule ^/100bestbuy/products/(.*)/$ 100bestbuy/search.php?categoryId=$1 [L,NC]

i am trying from 3 day but no result in localhost aswellas in server.
help me please…………….

Zen says:

perhaps you can try this:

RewriteRule ^100bestbuy/products/(.*)/$ 100bestbuy/search.php?categoryId=$1

and put the htaccess file in stage100floors.com root.

gozada says:

Hey, anybody can help me. I couldn’t able to do url rewriting.
here is my code of “.htaccess” file,

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^name/(.*)/$ rewrite.php?name=$1

thank you

Bill Wynne says:

Great tips using the htaccess file!

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

Leave a Reply

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Previous Post
«
Next Post
»