“Which one is best in search engine point of view? Hyphens or underscores?”
“I want to change the underscores in my urls to hyphens with modrewrite rule. How?”
If you have few urls with underscores to update use this simple method of 301 redirect.
Here are the answers …
Hyphens or underscores?
Matt cuttis from Google explained the answer to the first question in his blog. He explained dashes are preferable than underscores in urls.
http://www.mattcutts.com/blog/dashes-vs-underscores/
What are hyphens?
Hyphens are small lines ( – ) that you get when you press the key that is next to ” 0 ” key on your keyboard. Commonly we use it to represent a dash, minus sign and hyphen. But is is different for a computer programmer.
http://www.cs.sfu.ca/~ggbaker/reference/characters/
What are underscores?
An underscore is a symbol ( _ ) that you get when you press ‘shift + hyphen’ key. Usually used as an alternative to space key.
How to fix this underscore problem with your .htaccess?
If you have couple of urls to change follow this simple 301 redirect rule to update underscore urls to hyphenated urls.
What if you have some 35,000 urls to change? Like me … There comes the Apache modrewrite rule with regex usage.
.htaccess file is located in your root directory. Ftp into your web site and go to the folder where your sites index file is. You will see .htaccess file there.
If there is no .htaccess file there might be TWO reasons:
1. It is hidden. There will be an option in your ftp software to see the list of hidden files. Select it to see the .htaccess.
2. Your server is not actually created it when you create your web site first time. This is very rare situation but I have seen this. So ask your host to create one for you. Or create a text file on your computer called htaccess.txt and upload to your root directory. Rename it to .htaccess from htaccess.txt
Actual rewrite rules:
All my webmasters-central.com urls were with underscore initially when the directory was developed in 2002. Later I have decided to change the urls with ‘hyphens’ for search engine purpose.
On webmasters-central.com there are directories upto 9 levels and some 35,000 urls with underscores in it. So I created a rewrite rule with regex that converts the urls with underscores to hyphens.
Examples: (I didn’t change the extention of the files. They are still .shtml as before.)
For urls with ONE UNDERSCORE:
rewriteRule ^([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2.shtml [R=301,L]
For urls with TWO UNDERSCORE:
rewriteRule ^([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3.shtml [R=301,L]
For urls with THREE UNDERSCORE:
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4.shtml [R=301,L]
For urls with FOUR UNDERSCORE:
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4-$5.shtml [R=301,L]
For urls with FIVE UNDERSCORE:
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4-$5-$6.shtml [R=301,L]………………….
………………….
For urls with NINE UNDERSCORE:
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4-$5-$6-$7-$8-$9-$10.shtml [R=301,L]
Explanation:
The part ([^_]*) in the rewrite rule represents the word between or before or after the underscores.
Example:
http://www.webmasters-central.com/Affiliate_Program/index.shtml
It has one underscore in the url. So the first rule applies here.
([^_]*) represents ‘Affiliate’ part of the url. Server replaces underscore with hyphen and (.*) represents remaining url after last underscrore – ‘Program/index’
After finishing my rewrite rule, it will look like this: (COPY AND PASTE in a Notepad to see the code clearly)
RewriteEngine on
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4-$5-$6-$7-$8-$9-$10.shtml [R=301,L]
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4-$5-$6-$7-$8-$9.shtml [R=301,L]
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4-$5-$6-$7-$8.shtml [R=301,L]
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4-$5-$6-$7.shtml [R=301,L]
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4-$5-$6.shtml [R=301,L]
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4-$5.shtml [R=301,L]
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3-$4.shtml [R=301,L]
rewriteRule ^([^_]*)_([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2-$3.shtml [R=301,L]
rewriteRule ^([^_]*)_(.*)\.shtml$ http://www.webmasters-central.com/$1-$2.shtml [R=301,L].








[...] If you want to change some hundreds and thousands of urls with underscrore to hyphenated urls, check this tutorial. [...]
[...] Search engine optimization – hyphens or underscores? Htaccess modrewrite rule. [...]
[...] Search engine optimization – hyphens or underscores? Htaccess modrewrite rule. [...]