Apart from forwarding people, 301 redirects can also help you maintain a search engine friendly blog.
To handle URL redirects in your blog you have three options:
- Add rewrite rules in your .htaccess file (located in your blog’s root installation folder).
- Install plugins that will do it for you.
- Set the HTTP Header tags by PHP.
When would you want to add 301 redirects anyway?
Let’s see some example scenarios:Canonical URL
The problem – your blog can be accessed by two URL versions:http://www.yourblog.com/
and http://yourblog.com
. Search engines don’t like it because they consider your blog as two separate websites and therefore, duplicate content.You need to define a search engine friendly canonical URL, so your URL will be consistent (either with or without the WWW for all posts and pages in your blog).
The Solution – Canonical URL can be handled automatically by installing SEO plugins like All In One SEO Pack, which I already recommended in this post.
This plugin adds a canonical link tag inside your blog’s head tag which looks like this:
<link rel="canonical" href="http://www.webtechwise.com/ />
Another option to define canonical, is by adding rules to your htaccess file (between the # BEGIN WordPress and # END WordPress). For example:
RewriteCond %{HTTP_HOST} ^webtechwise.com [nc]
RewriteRule ^(.*)$ http://www.webtechwise.com/$1 [R=301,L]
You can use this nice online tool to generate yours.
Changing your blog’s domain name
The problem – you decide to change your blog’s name and URL.After installing your blog under your new domain name there are still many links pointing to your old domain, therefore, visitors will arrive to your old blog unless you redirect them to the new one.
You would also like to preserve the PageRank (and other ranks) you managed to achive so nicely for your old domain.
- The solution
- If your old domain is hosted in a separate account, add these lines to the .htaccess in the root directory of your old domain
RewriteRule ^(.∗)$ http://www.yournewdomain.com/$1 [R=301,L]
- If your old domain is a parked domain in your new domain’s hosting account use the following:
RewriteCond %{HTTP_HOST} !yournewdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.yournewdomain.com/$1 [L,R=301]
(it’s not a mistake that “yournewdomain” appears in both lines)
Changing post permalinks
The problem – after publishing an article, you decide to shorten its permalink (URL), to make it more search engine friendly.The thing is, your post is already indexed in Google (with its old URL), and received pingbacks and inbound links from other sites.
If you change your permalink without URL redirection, people and search engines that attempt to access your article, will receive a 404 error.
The solution – it happened to me twice since I launched this blog.
One example would be the redirection of this article from:
http://www.webtechwise.com/4-most-annoying-mistakes-to-avoid-when-building-a-clients-website/
to
http://www.webtechwise.com/web-designer-mistakes-with-clients/
See the difference between the two URLs?
The words “4″, “avoid”, “when building” are less relevant and therefore I’ve decided to remove them and keep a short permalink that includes only the important keywords.
To avoid 404 errors after changing the permalink, I simply added the following line in my htaccess file:
Redirect 301 /4-most-annoying-mistakes-to-avoid-when-building-a-clients-website/ http://www.webtechwise.com/web-designer-mistakes-with-clients/
Note that the second part has the domain name prefix (this may also work without it in some blogs but not in all, thanks to @Cool Solar Stuff for the comment).
Another solution to the problem would be to install the redirection plugin or AskApache Google 404 that would have handled this redirection automatically ,but in the meantime also consume resources and make my site a bit slower, so for 2 posts I preferred to do it manually.
Adding trailing slash and removing index.php
The problem – this is not really a major problem, however you can optimize your blog even more for search engines by adding trailing slash to your URL as well as redirecting requests for “http://www.yourblog.com/index.php” to “http://www.yourblog.com/”- The solution
- To add trailing slash:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.yourblog.com/$1/ [L,R=301] - To redirect “http://www.yourblog.com/index.php” to “http://www.yourblog.com/” type
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.yourblog.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Options +FollowSymlinks
RewriteEngine on
from http://www.webtechwise.com/url-redirection-in-wordpress/
参考:
http://www.webweaver.nu/html-tips/web-redirection.shtml
http://www.generateit.net/mod-rewrite/ (在线生成.htaccess)
http://www.askapache.com/htaccess/modrewrite-tips-tricks.html (
Htaccess Rewrites – Rewrite Tricks and Tips)
No comments:
Post a Comment