Mod rewrite tips for Yahoo, Google and other engines
February 9, 2005
Mod rewrite Tip 1
instant 301 from old dynamic url to new static url
(will only work if you use the exact variables from the dynamic url)
Old dynamic url:
somesite.com/catalog.php?cat=widgets&product_id=1234
New static url:
somesite.com/catalog/widgets-1234.html
#start .htaccess code
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^cat=([^&]+)&product_id=([^&]+)$
RewriteRule ^$ /catalog/%1-%2.html [R=301,L]
Reg Expression notes:
[^&]+ mean find any character except the "&" since it is what seperates the variables in a string. you can back reference matches in a rewrite condition using ()'s just like your rewrite rules but to call them you have to use a % instead of a $.
Benefits:
1. You don't have to hand write 1,000's of 301 redirects
2. Spiders can easily pick up the 301 and pass the scores and index the new urls much faster than having to crawl the entire site over from scratch.
3. Users clicking old dynamic urls in the SERPS will not get a 404 error. They'll go straight to the new static urls.
Mod rewrite Tip 2
Change a product name or change a mispelling and you've just lost all page scores to the static mod rewritten url. Unless...
Example url:
somesite.com/dinnerplates/cassa-stone/
Should be spelled
somesite.com/dinnerplates/casa-stone/
RewriteEngine On
RewriteBase /
# detect if the file being requested is a mispelling
RewriteCond %{REQUEST_URI} /.*cassa.*$
#redirect to the correct spelling using a 301
RewriteRule ^(.*)cassa(.*)$ /$1casa$2 [R=301,L]
Reg Expression notes:
.* means match any character 0 to infinite times. so .*cassa.* so zzzzcassazzzz would be a match as well as /cassa-stone/. All I'm doing is back referencing everything before and after cassa and then copying it into the new url and doing a redirect.
Benefits
This can really save your @$$ nuff said.
Mod rewrite Tip 3
Make the unormalized, normalized for Yahoo!'s sake.
Example code:
<a href="some-directory/">some directory</a>
All bots and SE's are different. ugh. Google is very good at indexing and scoring exactly what is in the <a href></a>tag whether the url is normalized or not. Yahoo on the other hand is a stickler for normalization and will index the example above like so in there SERPS somesite.com/some-directory
to keep a 404 from kicking in if some-directory needs to be picked up by your mod rewrite make sure you end all your RewriteRules that end their mathcing on directory/folder like so.
RewriteRule ^([^/]+)/?$ /somepage.php?name=$1 [L]
who thought a question mark would ever be an answer
? mean match the previous character 0 to 1 times. and will fix any normalization problems you might have encountered with Y!.
#This will send you into a horrible loop and bad thing may happen :(
#so don't use this
RewriteRule ^(.*)$ /somepage.php?name=$1 [L]
Benefits
1. users will not hit a 404 when clicking a yahoo SERP
2. worth its weight in gold!
3. Your site will not look like a bad cloak hehe
Posted by seomike at Search Engine Watch Forums and at Spider Food Forums.
instant 301 from old dynamic url to new static url
(will only work if you use the exact variables from the dynamic url)
Old dynamic url:
somesite.com/catalog.php?cat=widgets&product_id=1234
New static url:
somesite.com/catalog/widgets-1234.html
#start .htaccess code
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^cat=([^&]+)&product_id=([^&]+)$
RewriteRule ^$ /catalog/%1-%2.html [R=301,L]
Reg Expression notes:
[^&]+ mean find any character except the "&" since it is what seperates the variables in a string. you can back reference matches in a rewrite condition using ()'s just like your rewrite rules but to call them you have to use a % instead of a $.
Benefits:
1. You don't have to hand write 1,000's of 301 redirects
2. Spiders can easily pick up the 301 and pass the scores and index the new urls much faster than having to crawl the entire site over from scratch.
3. Users clicking old dynamic urls in the SERPS will not get a 404 error. They'll go straight to the new static urls.
Mod rewrite Tip 2
Change a product name or change a mispelling and you've just lost all page scores to the static mod rewritten url. Unless...
Example url:
somesite.com/dinnerplates/cassa-stone/
Should be spelled
somesite.com/dinnerplates/casa-stone/
RewriteEngine On
RewriteBase /
# detect if the file being requested is a mispelling
RewriteCond %{REQUEST_URI} /.*cassa.*$
#redirect to the correct spelling using a 301
RewriteRule ^(.*)cassa(.*)$ /$1casa$2 [R=301,L]
Reg Expression notes:
.* means match any character 0 to infinite times. so .*cassa.* so zzzzcassazzzz would be a match as well as /cassa-stone/. All I'm doing is back referencing everything before and after cassa and then copying it into the new url and doing a redirect.
Benefits
This can really save your @$$ nuff said.
Mod rewrite Tip 3
Make the unormalized, normalized for Yahoo!'s sake.
Example code:
<a href="some-directory/">some directory</a>
All bots and SE's are different. ugh. Google is very good at indexing and scoring exactly what is in the <a href></a>tag whether the url is normalized or not. Yahoo on the other hand is a stickler for normalization and will index the example above like so in there SERPS somesite.com/some-directory
to keep a 404 from kicking in if some-directory needs to be picked up by your mod rewrite make sure you end all your RewriteRules that end their mathcing on directory/folder like so.
RewriteRule ^([^/]+)/?$ /somepage.php?name=$1 [L]
who thought a question mark would ever be an answer
? mean match the previous character 0 to 1 times. and will fix any normalization problems you might have encountered with Y!.
#This will send you into a horrible loop and bad thing may happen :(
#so don't use this
RewriteRule ^(.*)$ /somepage.php?name=$1 [L]
Benefits
1. users will not hit a 404 when clicking a yahoo SERP
2. worth its weight in gold!
3. Your site will not look like a bad cloak hehe
Posted by seomike at Search Engine Watch Forums and at Spider Food Forums.



