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 • 120,913 visits 55 comments
Tags : ,


or Subscribe to specific category only :




  - 55 Comments


Toronto Internet Marketing says:

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.

Dhruv Kumar S says:

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.

Abhi says:

Hey what to change in rewrite.php file ? thanks in advance

news and updates says:

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

news and updates says:

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

Real Estate Analysis says:

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

Karnataka Medical says:

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.

medical assistant says:

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″

Milos says:

Hello, it’s a very good post. I have a little problem:
My pages is:
index.php?=detalji?265
index.php?=detalji?112
index.php?=detalji?221

I want rewrite this pages as:
article-265.html
article-112.html
article-221.html

I made a .htaccess file like this:
Options +FollowSymLinks

RewriteEngine On
RewriteRule ^article-([0-9]+)\.html$ index.php?=detalji?$1

But when i tipe a http://www.lcd.rs/article-221.html I give onli index.php page, not index.php?=detalji?221

Do you now where I make a mistake?

Tnx a lot,
Milos Popovic

learning and teaching says:

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

a.r.bhuyan says:

its very important issue for seo learner .thanks for this post.

Bill Wynne says:

Great tips using the htaccess file!

Zeeshan Parvez says:

Just what the doctor ordered! Exactly what I was looking for to integrate with my cms. I needed a way to get seo friendly urls. Now I have the answer. Thanks!

Jaybee says:

Hi,
I have scanned the internet for a mod rewrite solution to turn my urls from:
http://www.domain.com?section=this&catid=1&articleid=1
to
http://www.domain.com/about-us/the-team (like they do in WordPress)
And I do not want to pass numbers in the urls.

It has forced me to hard code all my dynamic links in my htaccess which becomes tiresome when you add more content onto the website because this method is not automated. eg.

————————————————–
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^the-team$ index.php?section=this&catid=1&articleid=15 [NC,L]
————————————————–

Please help me come up with a workable automating solution just like they do in WordPress. Note: All the content is stored in a database.

google places says:

Very useful list Zen, .htacces is something I’ve always struggled with and this has helped me get my head around it a bit. Before it was trial and error really but this has helped me understand why I’m actually using each separate line of code. Cheers

mitra says:

Hi Zen, really its a very good tutorial…..
But I have a problem with url rewriting.
My present link is
http://localhost/htaccess/products/product.php?pname=computer&id=5
and I want http://localhost/htaccess/products/computer/5/
So how can I do it? and what should I write within href=”"?
basic folder structure is —
root/products/product.php
root/customers/customer.php
And presently the hyperlink file is within index.php at root folder.

And another thing, there are similar pages like product.php and corresponding different parameters, suppose—
http://localhost/htaccess/customers/customer.php?cusname=mitra&id=10
and I need as same as above
http://localhost/htaccess/customer/mitra/10
So is it possible to write just one rewriteRule to do these all?
if yes, then how?

Thank you.

Arvind says:

Hi,

I have been trying on localhost to rewrite this URL (http://localhost/cms/categories.php?id=1) to (http://localhost/cms/category/1)

I used the following rewrite condition (and also others) in .htaccess file but everytime it has been failed:

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^cms/category/([0-9]+)$ /cms/categories.php?id=$1 [R,L]

can anybody help me to tell where I am wrong?

Thanks for help in advance

arif says:

i have a url like this

http://abc.local/stock/detail.php?id=1 i need to change this like

http://abc.local/stock/product-title.html

i need to show product title which will come from the database

kindly give me a example of my problem

thanks

Toko Online says:

Nice, with htaccess rewrite rule every web url looks easy.
Thanks

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

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

Previous Post
«
Next Post
»