Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

Redirect WordPress Date Archives with .htaccess

Restructuring a WordPress website may involve removing the subdomain from URLs/permalinks. For example, I recently removed the original WP-install subdirectory from Perishable Press to simplify site structure and optimize WordPress permalinks. There are PHP scripts and WP plugins that might work for this, but in most cases .htaccess is optimal when changing URL structure and redirecting traffic. Here’s a quick example to help visualize the concept:

Old structure (w/ subdomain)
https://perishablepress.com/press/2011/
https://perishablepress.com/press/2011/02/
https://perishablepress.com/press/2011/02/15/

New structure (w/out subdomain)
https://perishablepress.com/2011/
https://perishablepress.com/2011/02/
https://perishablepress.com/2011/02/15/

We also want to redirect date-archive permalinks when they’re paged or missing the trailing slash. Note that here we’re dealing with WordPress date archives, which is different than simply removing the “year/month/day” from your single-post permalinks. We’ll get to doing that a bit later, for now let’s remove the subdirectory from the date-based archives.

Redirecting date archives

First, if you’re familiar with using .htaccess, you may be asking, “why is this even necessary? Why not just redirect *everything* from the subdirectory?” Good question. If you have WP installed in a subdirectory structure like this:

example.com/wordpress/
example.com/wordpress/wp-admin/
example.com/wordpress/wp-content/
example.com/wordpress/wp-includes/
...

..and want to redirect to basically the same thing, only without the wordpress subdirectory:

example.com/
example.com/wp-admin/
example.com/wp-content/
example.com/wp-includes/
...

such that there is basically a one-to-one correspondence between old and new files, then YES you can just redirect everything wholesale with a simple line of .htaccess:

RedirectMatch 301 ^/wordpress/(.*) http://example.com/$1

and be done with it — no need to bother with the other redirect methods in this tutorial. If, however, your site restructuring is more complicated, perhaps involving stuff like:

  • you also want to remove the year/month/day from your single-post permalinks
  • you are removing/renaming/consolidating directories, so not 1-to-1
  • you are changing tag/category/taxonomy names, so not 1-to-1
  • other subdirectory installations of WordPress are involved
  • you’re just nuts for even thinking about this crazy stuff

If any of these apply, then the easy way is out and you’ve got some thinking to do. I’m here to help with that, starting with redirecting your date-based archives. Here is the magic code to add to your site’s root .htaccess file (after making a backup copy of the original):

# REDIRECT WP DATE ARCHIVES
RedirectMatch 301 ^/wordpress/([0-9]+)/?$         http://example.com/$1/
RedirectMatch 301 ^/wordpress/([0-9]+)/page/(.*)$ http://example.com/$1/page/$2

RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/?$         http://example.com/$1/$2/
RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/page/(.*)$ http://example.com/$1/$2/page/$3

RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/([0-9]+)/?$         http://example.com/$1/$2/$3/
RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/([0-9]+)/page/(.*)$ http://example.com/$1/$2/$3/page/$4

There are three sections to this code: yearly archives, monthly archives, and daily archives. The only editing that you need to do is change the “wordpress” in each line to match your subdirectory’s name, and then also replace all instances of example.com with your domain name.

Eliminate all date-based archives

You may have asked at some point, “why bother at all with date-based archives?” After all, it’s redundant info that may contribute to “duplicate-content” penalties in search-engine results. I think most blogs these days emphasize categories and tags over the myriad other archive types. Many themes tend to ignore the date-based archives and popular SEO plugins provide easy ways of no-indexing/removing them from search engines. So let’s just eliminate date archives entirely, say by redirecting all of it to your home page, where the additional link equity may provide some SEO benefits to your site. So that’s the spiel — here’s how to do it..

Step 1

Add the following code to your site’s root .htaccess file:

# REDIRECT DATE ARCHIVES TO HOME PAGE
RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)?/?([0-9]+)?/?$    http://example.com/
RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)?/?([0-9]+)?/?page http://example.com/

As discussed in the previous section, edit or remove the subdomain to match your own, and then change the example.com to whatever makes sense (e.g., your home page). Test thoroughly using as many different archive/permalink formats as possible.

Step 2

Once the date archives have been redirected via .htaccess, you should go through your theme and remove/edit any date-based archive links and/or code. A typical location for such would be archive.php, archives.php, index.php, and of course date.php if it actually exists. Once your theme is cleaned up, you’re all set, but should keep an eye on any weird activity in your traffic logs. As straightforward as it seems, it’s a big move, and you want to be careful.

Removing year/month/day from single-post permalinks

Removing the year/month/day from your single-post permalinks is another way to improve the SEO integrity of your WordPress-powered site. Here’s the code to do it via .htaccess:

# REMOVE DATE FROM WP POST PERMALINKS
RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://example.com/$4

As-is, this technique also removes the subdirectory (/wordpress/), so edit accordingly or remove if not needed. Once in place, this code will change your single-view permalinks from overkill:

http://example.com/wordpress/2012/04/12/post-name/

..to elegance:

http://example.com/post-name/

Remember to test thoroughly after making any changes! As always, questions & comments welcome. Thanks for reading :)

About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
Digging Into WordPress: Take your WordPress skills to the next level.

19 responses to “Redirect WordPress Date Archives with .htaccess”

  1. airoschou 2012/04/16 7:34 pm

    often redirect can be bad for google Indexed.

  2. Sorry, i means often modify the url structure, may be the search engine will be unfriendly.

  3. Matt McDowell 2012/04/17 9:30 am

    Could this technique be adapted to do things like removing the category base from the url? Something like:

    RedirectMatch 301 ^/category/(.*)$ http://example.com/category/$1

    • Jeff Starr 2012/04/17 9:42 am

      In theory yes it should work the same way.. the question is how will it affect WP functionality. I’ve played with trying to remove category and category-parent slugs from the URL but no success.

  4. Bharat Chowdary 2012/04/19 2:28 pm

    Finally got the code I’m looking for. Thanks Jeff.

  5. Thank ya kindly homeslice. The redirects work perfectly!

  6. airoschou 2012/05/28 2:39 am

    hello,jeff:

    As this post, I have a question, long long time ago, i have a link structure look as:

    http://example.com/blog/web-design-blog/

    Now,i want to remove the /web-design-blog/

    how can i use your method to remove it

    it’s important to me, hope your help,i’ll be very grateful

    • Jeff Starr 2012/05/28 2:45 am

      If I understand correctly, something like this should work:

      RedirectMatch 301 /blog/web-design-blog/ http://example.com/blog/

      More tuning will probably be needed, but that’s the basic idea.

      Editors note: Just finished switching servers and had to recreate some comments that were posted on the old server. Just in case you were wondering.

      • airoschou 2012/05/28 2:49 am

        haha, thanks
        i use

        RedirectMatch 301 ^/blog/web-design-blog/(.*)$ http://example.com/blog/$1

        the problem is OK, thanks again~

  7. Thanks for the post. I installed my blog in the wordpress folder so, at present, I have a redirect for those who type example.com to be redirected to example/wordpress.com

    How can I keep that redirect and institute this for posts
    RedirectMatch 301 ^/wordpress/(.*) http://example.com/$1

  8. Thanks Jeff, Finally the code I needed….

  9. Sven Raphael Schneider 2012/10/16 2:34 pm

    Great tutorial Jeff. Just to be sure. I am working on a blog right now and I want to redirect the permalinks from:

    http://blogs.example.com/2012/04/12/post-name/

    to:

    http://blogs.example.com/category/post-name/

    Does the correct redirect look like this?

    # REMOVE DATE FROM WP POST PERMALINKS
    RedirectMatch 301 ^([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://example.com/category/$4

    I am sure it does not because we the date does not correspond with the individual categories, does it? Is there even a way to solve that issue with .htaccess ?

    • Correct, in order for the category to be used in the target URI, it must be available in the initial request. I would set up a test install of WP, create your original (old) permalinks, then change them to include/use the category. The point is, WordPress should automatically take care of the redirects for new and existing content. It’s only for external/non-WP links that will need to be manually redirected. In this case, .htaccess may not suffice, but PHP should get you there.

  10. This is what i was looking for long time.Thanks for the code and i will fix permalink related issues.

  11. Hi, I have some problems with custom post types (WP) URL. I cannot find a solution.

    The problem is as follows:

    http://example/custom-post-type/url-file/?utm_source

    My theme has generated automatic URL’S for each custom post type.

    I can not bring back from these addresses to the original article. If I create a folder and add there a htaccess works but there is no direction for search engines or social networks.

    How can I fix this? Is there a way?

  12. Thanks! Exactly what I needed to do some redirect magic. As a general blog, we still prefer to keep our date based archives for easy reference.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
SAC Pro: Unlimited chats.
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.