URL Redirection in WordPress – the Problem Solution Approach
March 29th, 2010 by
301 redirects are rules that automatically forward visitors from one page (or website) to another.
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]
Note: Don’t forget to add the following before htaccess rules (if they don’t exist already)
Options +FollowSymlinks
RewriteEngine on
Any questions or suggestions? Feel free to speak your mind
Additional resources
Respond Here
33 responses
Trackbacks and Pingacks
- [User Link:URL Redirection in WordPress – the Problem Solution Approach] | Tips for Designers and Developers | tripwire magazine
- Can you buy domains from Wordpress directly?
- 150+ Need to Check Out Fresh Articles for Designers and Developers | tripwire magazine
- wp-popular.com » Blog Archive » URL Redirection in WordPress – the Problem Solution Approach | WebTechWise
- Blogging Tips, DoFollow Debate, and Web Design Tips Weekly Round Up April 05, 2009 | Evolutionary Designs
- 16 Incredible Wordpress Plugins You Wish You Knew
- 如何解决Google Webmaster Tools中的Sitemap “Paths Don’t Match” 错误 | mr.wei

Great post Omer! Any suggestions for how to handle affiliate links in this manner?
Hi Keller, I didn’t have a chance to work with affiliate links yet, but I sure plan to.
I assume the same solutions apply to those links as well
Wow, I’m not a webmaster but this post sure helped me understand this issue. Someone asked me on Twitter the other day about the difference between 301 and 404 redirect and I was like – Huh???
Now I know where to send them and because of the way you right and the great examples you gave I actually understand what this is all about.
I have one question which I think I can get the answer for in one of your resource links (don’t laugh remember I’m new to all of this). Just what is the htaccess file and where does it reside? I know I’ve seen it referenced in a thousand posts so now is the time I learn about it. Thanks Omer!
@Ileane
Hey Ileane, thanks for your feedback. It’s great to hear my article is clear.
For WordPress.org blogs (self hosted blogs that are not part of WordPress.com) – .htaccess is a file located in your WordPress installation folder (the same folder that contains the subfolders wp-content, wp-admin and the configuration file wp-config.php).
It is responsible for various configurations such as URL redirections, security definitions (to disable malicious access to your blog’s files and folders), 404 error pages and more.
.htaccess comes built in with WordPress installation, but you can customize it as explained in this post.
Great tips, Omer.
When I changed my domain last month, I had to google from many sources to do the 301 redirects. This post will save time for many bloggers someday.
Thanks
changing a domain can be a headache with all those redirects. But if you took care of it it’s great
Nice explanation. I’ll bookmark this for now. I’m sure it’ll be useful to me sometime.
For some reason, even with All in One setup to add a canonical link to my site, I can still access either www or no-www. Before, that never happened. Should I use the .htaccess rule as well?
Hi Tony, if you have the AIO SEO plugin installed, every page in your blog should have a block of three lines in its source generated by the plugin. The lines are meta description tag, meta keywords tag and link canonical tag.
When you right click on your page and select “View Source”, you don’t see any of those lines? (you can also search for “all in one seo” in the source and find them).
If you still don’t see it, check that the canonical url check box in the plugin options page is ticked, and of course that the plugin is set to activated.
I see those lines. Something like this:
<!– All in One SEO Pack 1.6.10.2 by Michael Torbert of Semper Fi Web Design[305,323] –>
<!– /all in one seo pack –>
On the last line, I see the canonical link set to no-www. Problem still persists. I check the AIO plugin page and see the canonical url option clicked.
Tony I fixed your comment to show code.
It is strange that AIO does not generate the canonical for you. Have you tried uninstalling it and then reinstalling?
Anyway, using the .htaccess code for canonical should work as well.
You can also read this post http://bit.ly/dsr8R that discusses this bug you have and possible solutions.
Another solution would be to add a this line somewhere inside the HTML head tag:
<?php if ( is_singular() ) echo ‘<link rel="canonical" href="’ . get_permalink() . ‘" />’; ?>
Hey, Omer. When I tried the .htaccess method, everytime I enter the www url, I get something appended to the url that looks like this:
(link deleted due to invalid page)
It seems to be getting the cached version of the page?
Hey Tony, did you try the PHP solution as well? maybe it will not mess up the URL.
If it does, I guess there is something wrong with your htaccess file. If you want, you can email it to me (you have my email) and I’ll check it.
Hi there,
I am visiting a website in another country, then they decided to redirect those entring their website from abroad to another version, while the original version is still runing for those users within that country. can I bypass the redirect and enter the website as if I am inside user?
thanks
jane klose
made2b4u@hotmail.com
Hi Jane, thanks for commenting. Does this website have a different URL for people entering from abroad?
Either way, there you might be able to connect to the website through a proxy server that is located in the same country as the web host.
This subject always gets me confused. Omer, when you put your link out on the web do you always include the trailing slash? With regards to PageRank do you think .com could have a higher PR than .com/ or doesn’t it matter?
Lou
Lou, missing the trailing slash is handled differently by servers as explained here http://bit.ly/4c0isN
Nice post. People don’t care much about the trailing slash, but it’s a mistake. Google has announced that the trailing slash can make the content of the website duplicated. Your solutions here are very good and worth a look. Thanks for sharing.
I’m glad you like the article, thanks
Hi, I recently changed the blog address of the website but I still have some minor issues on it, mostly the backoffice and login create problems because they still direct to the wordpress address. Do you perhaps know something to change the backoffice url to the same url I use for the blog?
Hi Joris, you need to login to your PHPmyAdmin and then navigate to the db options table and change the siteurl and home values to match your new url. Let me know if it works for you
Thx a lot Omer, it worked out perfectly.
Tip: when I used your example:
Redirect 301 /4-most-annoying-mistakes-to-avoid-when-building-a-clients-website/ /web-designer-mistakes-with-clients/
I got a 500 server error… so I added http://www.mydomain.com to the 2nd url and now it works:
Redirect 301 /4-most-annoying-mistakes-to-avoid-when-building-a-clients-website/ http://www.webtechwise.com/web-designer-mistakes-with-clients/
Maybe this will help others as well..
You are right, thanks for pointing that out.
I will correct it right now.
Recently I have deleteda category permanently.Now that category listed in mthe google search.when some one click on that it shows page not found error.what I want is to redirect that categary url to a some other external site url.Please help me on this.Hasini, I didnt have a chance to redirect categories.
Did you try this plugin? http://wordpress.org/extend/plugins/category-redirect-to-post-or-page/
Hi guys, I had a weird problem related to these topics. I hope someone will be able to help me? I didn’t like the URL of a page so I changed it for SEO purposes, but I actually wanted Google to delete the old URL / page because it was not performing well at all. The trouble is that Google Webmaster’s Tools would delete a page from the index only if it returns 404 and the old URL was redirecting me to a similar blog post?? So I had to delete also the blog post to get the 404 error. How can I avoid WP automatically redirecting and decide myself when I want a redirection or not? Many thanks in advance.
It seems that the problem you present here is more GWT related then the redirection. However, why do you say you need to get a 404 error to remove a page from Google index?
Last time I checked GWT lets you remove any page you want from its index. What message did you get when you tried to remove that page?
A really helpful post about URL redirection in WP. Just what I’m looking for! Thanks for posting this Omer!
You’re most welcome Mummy. I’m glad it helped you.
Amazing!! You saved me hours of research! This is the best 404 redirect info I have read online! I gave you a shout out on Google+
This little piece of code helped alot too
You put it in the 404.php in your wordpress theme file so you don’t have to use .htaccess but in my case I needed both!
Thank you again, excellent post!
Does WordPress now automatically redirect URLs? For instance:
I have the following URL: mydomain.com/how-to-go-to-the-mall/
I change the URL to: mydomain.com/how-to-get-to-the-mall/
When I publish that change through the edit function on the post page (editing the slug), and visit the old URL, it auto-redirects me to the new URL. That happens with all my posts, I was just wondering if I did something by accident to allow that or it was basic now.