Redirect Day and name permalinks to /%postname%/

If you use the ugly /%year%/%monthnum%/%day%/%postname%/ permalink structure, you should be interested to know how you can switch to the elegant /%postname%/ structure without loosing any backlink, using a simple .htaccess redirection.

The first thing to do is to login to your WordPress admin, go to Settings → Permalinks and select custom. Fill out the field with /%postname%/.
Your permalinks will now look like the ones on this blog:

http://www.yourblog.com/name-of-the-post

Now we got to redirect all backlinks using the old permalinks structure to the new permalink structure. To do so, you'll have to edit the .htaccess file, located in WordPress root directory.
Be careful while editing .htaccess: Always create a backup before!

Paste the following line in your .htaccess:

RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.domain.com/$4

Allright! You just changed your permalinks structure without loosing any backlinks!

Credits goes to Joost de Valk newsletter for this awesome recipe!

37 Responses

Nov 07 2008 23:37

OK, that’s really handy to know. But here’s a question: can you do the reverse, in other words move from %postname% only to %year%/%month%/%day%/%postname%, for example. Is it just a case of swapping the two parts of the redirect code around?

Nov 07 2008 23:48

Hi Paul, I’m not totally sure about it, but I don’t think that it’s possible to redirect %postname% to %year%/%month%/%day%/%postname%.

Nov 08 2008 04:42

Thought it might not be - more than one post with the same name would be a problem, for example. But I can’t help but feel there must be a way to do it somehow.

Nov 08 2008 05:54

Could you tell us more precisely where to paste the line in the htaccess file?

I never edit it!

Thanks!

Nov 08 2008 09:14

@Zhu: You can append the line at the end of the .htaccess file. Use the # symbol to write comments. ANd remeber to always create a backup of your .htacess file before editing it. :)

Nov 08 2008 14:06

Thanks for the share on this. I am currently using the ugly domain address for my blog article. Looks like it is time to change.

Wei Liang

Nov 09 2008 22:41

What a simple solution… I used the link migration plugin, it does the thing but this solution seems to be a lot easier!

Nov 11 2008 08:16

Just to be sure…

This is my htaccess:

# BEGIN FAlbum

# END FAlbum
# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Should I just add:

# RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.domain.com/$4

right after # END WordPress?

Sorry for asking again, I’m not familiar with editing htaccess at all!

Thanks ;-)

Nov 11 2008 09:54

@Zhu: Yes :) Don’t forget the $ after domain.com, or it will not work at all ;)

Nov 11 2008 20:58

Thank you so much!

I learned something today :-)

Nov 11 2008 21:15

@Zhu: You’re welcome :)

Nov 15 2008 12:12

Hey JBJ…I have a question.

My situation is slightly different from what you described. I have a HUGE website with about 3000 unique visitors per day, where permalinks follow none of these two formats, they’re just numeric, like http://www.example.com/234 and I would like to switch to http://www.example.com/year/month/postname without losing any backlinks and without causing high load on my server. Would that be possible and what would be the code for this.

Nov 19 2008 21:58

Just curious on one part of this…In the code, it has http://www.domain.com only. How would you code this to use both the domain name with and without the www?

Nov 19 2008 21:59

@Sue: It works exactly the same way, you just have to remove the http://www.

Nov 19 2008 22:01

@Iva: Sorry for the late reply…I don’t think this should be possible, sadly.

Nov 19 2008 22:07

Wow, jbj. That was quick. :)

But what I meant was having both in there. Would you separate the two with a comma, or do you have to add a totally separate line in the .htaccess for the non-www domain?

Nov 19 2008 22:19

I’m not sure about it, but maybe a separate line for the non-www domain should work. Tell me if it doesn’t!

Dec 12 2008 18:57

Sue,

First a quick disclaimer: I barely know how to tie my own shoes, let alone coding language. I’m a noob (at best).

But in spite of that, I’ve learned a few things along the way. I have the following on my .htaccess to deal with the www no http://www.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

I’m about to use the awesome hack from this post, and I imagine because of the above, there won’t be any reason to worry about the www no http://www. If I’m wrong, I’ll come back to let you know.

jbj,

Great site! Thanks for all the valuable info! I had found a different set of instructions and method to do this, but was hesitating due to too many steps and additional plugins needed. Thanks for making it a much simpler, straight-forward process!

Dec 12 2008 19:09

@theCL: I plan to write a recipe about redirecting your site to www/non-www soon, so stay tuned :)
Thanks for the kind words, glad you like the site!

Dec 25 2008 10:05

jbj,

I was thinking about doing this from long time. I have one question, what happens to all pages that are already indexed with old permalink structure by search engines like google? Will this permalink structure change won’t make google angry and remove the url from the search results?

Jun 04 2009 15:59

Hi, I’m digging up this old post because I’m currently lost with redirects. I’ve moved my website to a new host but would like to keep the backlinks and can’t seem to be able to do it in htaccess.
Eg:
http://blog.sachin-db.com/2007/02/23/logos/ now gives a 404 but the post is still here at this address:
http://www.sachin-db.net/logos

As you can see, the domain has changed and I have removed the dates from the rest of the URL but I can’t seem to be able to make any redirect now.

Could you please help?
Cheers
Sachin

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required
Blog And Make Cash