Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
PHP ›
Redirecting WordPress archives to a page

Redirecting WordPress archives to a page

June 26, 2013 PHP Jesin A 26 Comments

php category thumbnail

WordPress started as a blogging platform but soon evolved into a Content Management System (CMS) used to create full websites. But certain features meant for a blog doesn’t suit well for websites and one among them is displaying archives. An archive in a blog contains a list of “posts” based on date, category or author, sometimes even bloggers (like me) don’t like to displaying date archives. So I decided to redirect the date archives in my blog to the home page without using any plugin and in the process also learnt redirecting category archives to other wordpress pages also. There are many ways to do this like editing the .htaccess file or using a plugin and here I’ll be editing the wordpress PHP theme files.

Before we begin let me tell you that this method will stop working if you change your theme, so you need to add the code again if you decide to change the wordpress theme. Please take a backup of your theme’s archive.php file located in the following location.

/document/root/wordpress/wp-content/themes/theme-name/archive.php

Replace “/document/root/wordpress” with your WordPress installation’s document root and replace “theme-name” with your “current” theme’s actual name.

Login to your WordPress /wp-admin as an administrator. Go to Appearance > Editor and from the right templates list click “Archives” the file name is usually archive.php.

wordpress edit themes
Login to wp-admin as an administrator, navigate to Appearance > Editor and click on “Archives” from the right side under Templates

Choose and insert the appropriate code based on what and where you want to redirect.

Redirect date archives to home page

This is what I’ve done on my blog, this is what happens

https://websistent.com/2012/ >>redirects to>> https://websistent.com
https://websistent.com/2012/03/ >>redirects to>> https://websistent.com

Insert the following code at the beginning of the “Archives” theme file.

<?php
if(is_date()) {
$homepg=get_bloginfo('url');
header("Location: $homepg",TRUE,301);
}
?>

The is_date() wordpress function checks if the currently displayed archive is a “date archive” and returns TRUE or FALSE. A date archive is the one that displays posts by the date mentioned in the URL, so http://www.example.com/2012/ will display all posts published in the year 2012. The next line contains another wordpress function get_bloginfo(‘url’) this returns the “Site address (URL)” set in Settings > General page.

wordpress get_bloginfo url
The wordpress function get_bloginfo(‘url’) return the link set in the “Site Address (URL)” field in the Settings > General

The header() is a PHP function using which we are sending a 301 “Permanently Moved” HTTP header with the home page in the “Location:” header.

Redirect date archives to a page

If you want date archives to the redirected to a particular page like these

https://websistent.com/2012/ >>redirects to>> https://websistent.com/sitemap/
https://websistent.com/2012/03/ >>redirects to>> https://websistent.com/sitemap/

Insert the following code at the beginning of the “Archives” theme file.

<?php
if(is_date()) {
$pagelink=get_page_link (get_page_by_title( 'Page Title' ));
header("Location: $pagelink",TRUE,301);
}
?>

Replace the text “Page Title” with the title of the page to be displayed. The get_page_by_link() wordpress function retrives the page ID of the “Page Title” passed as a parameter, this page ID is passed as a parameter to the get_page_link() wordpress function which retrieves the permalink of that page.

Redirect category archive to a page

This is what happens in here.

https://websistent.com/category/windows/ >>redirects to>> https://websistent.com/sitemap/
https://websistent.com/category/linux/ >>redirects to>> https://websistent.com/sitemap/

Insert the following code at the beginning of the “Archives” theme file.

<?php
if(is_category()) {
$pagelink=get_page_link (get_page_by_title( 'Page Title' ));
header("Location: $pagelink",TRUE,301);
}
?>

Use the is_category() wordpress function to check if the currently displayed archive is a category. Replace “Page Title” with the title of the page to be redirected.

Redirect a specific category archive to a page

Finally this is for redirecting a specific categories to specific pages.

https://websistent.com/category/windows/ >>redirects to>> https://websistent.com/sitemap/
https://websistent.com/category/linux/ >>redirects to>> https://websistent.com/contact/

The code for this is similar to the one mentioned previously the only difference is the pass the “Category Title” as a parameter to the is_category() function.

<?php
if(is_category( 'Category Title' )) {
$pagelink=get_page_link (get_page_by_title( 'Page Title' ));
header("Location: $pagelink",TRUE,301);
}
?>

The words “Page Title” in line number 3 should be modified to the page you want the archive to be redirected. You can just continue this code with an elseif and add more “Category Titles”.

Redirect author archives

Author archives display posts written by a particular author, this may not be necessary if you have a single author blog or building a website.

https://websistent.com/author/a.jesin/ >>redirects to>> https://websistent.com

Insert the following code in the beginning of the archives.php file.

<?php
if(is_author()) {
$homepg=get_bloginfo('url');
header("Location: $homepg",TRUE,301);
}
?>

The is_author() wordpress function redirects all requests to author archives to the homepage. You can also pass an author’s nickname as a parameter to the is_author() function so that only those author’s archives redirect. An author’s nickname is the one entered in Users > Your Profile.

<?php
if(is_author('Author Nickname')) {
$homepg=get_bloginfo('url');
header("Location: $homepg",TRUE,301);
}
?>

Redirection can also be done to a different page instead of the home page.

https://websistent.com/author/a.jesin/ >>redirects to>> https://websistent.com/about-me/

To achieve this use the following code.

<?php
if(is_author( 'Author Nickname' )) {
$pagelink=get_page_link (get_page_by_title( 'Page Title' ));
header("Location: $pagelink",TRUE,301);
}
?>

Now go ahead and test what you’ve done.

Testing and Troubleshooting

After everything is done open up your archive page and check if it redirects properly to the required page. After that is done use the HTTP Header tool to check if the correct response code is being sent as this is crucial for search engines. You’ll see headers similar to the following

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
X-Pingback: https://websistent.com/xmlrpc.php
Location: https://websistent.com
Server: Chitti v2.0
Content-Length: 23336
Accept-Ranges: bytes
Date: Tue, 25 Jun 2013 15:53:40 GMT
Connection: close

The response code must be 301 and the “Location:” should the target page you mentioned in the header() function. If you get a 404 page not found error check the URL mentioned in the “Location:” header by copy pasting it into your browser’s address bar.

If you get a browser error “This page isn’t redirecting properly” its because the “Location” header contained nothing, this could be due to a non existent “page title” entered in the PHP code in archive.php. So check if the case and spelling of the “Page Title” is correct.

Still facing problems redirecting? Drop a comment and I’ll help you out.

Related posts:

wordpress custom 403 error pageCustom 403 and 401 error pages in WordPress php category thumbnailCreating a PHP MySQL Login Page Script Default ThumbnailRedirect non www to www Default ThumbnailSetting a Timezone in PHP How to customize the BuddyPress activation email

Tags: php, redirect, wordpress

Comments

  1. mary says

    October 25, 2013 at 7:44 pm

    Hello,

    I am trying to redirect a specific tag archive (e.g http://mydomain.com/tag/tag-name/) to a specific (External) URL. Is that possible, and if yes, do you know how to do it?

    Actually, I want to redirect many tags to the same external url.

    Thank you in advance

    Reply
    • Jesin A says

      October 25, 2013 at 11:27 pm

      Hi Mary,

      This is possible using the is_tag() function. Pass the tag slug as an argument.

      <?php
      if( is_tag( 'tag-name' ) )
      {
      	header("Location:  <externalsite>", TRUE, 301);
      }
      ?>
      

      To redirect many tags to the same URL use the OR logical operator.

      <?php
      if( is_tag( 'tag-name' ) || is_tag( 'tag-2-name' ) || is_tag( 'tag-3-name' ) )
      {
      	header("Location: <externalsite>", TRUE, 301);
      }
      ?>
      
      Reply
      • Tushar Sirwani says

        October 6, 2017 at 1:28 am

        Thanks alot Jesin A it worked!

        Reply
  2. mary says

    October 26, 2013 at 1:34 pm

    It worked! Thank you soooooooooooooooo very much!

    Reply
    • Jesin A says

      October 26, 2013 at 9:41 pm

      Great to hear that!!!

      Reply
  3. Dorian says

    January 16, 2014 at 12:58 pm

    Hi Jesin,
    Thanks a lot for this tutorial! I’ve been looking for a way to do this for ages.

    I just tried to implement your advice on my website with the category archive pages, but something goes wrong: instead of redirecting me, the page goes completely blank, and the URL remains that of the category archive page.

    This is the original structure of my archive template page (it’s bilingual Eng/Chinese):

    <a href=""><img src="/images/house.png" alt="Home" /> » fill();
    $breadcrumb_trail->display();
    }
    ?>

    create_bookmarks();?>

    <div class="post" id="post-" >

    <a href="" rel="bookmark">

    |

    ...(etc.)...

    I tried inserting the following code after the “$themePath” line above:

    What is it you think I might be doing wrong?
    I tried adding an “endif;” at the end of that last bit of code, but no joy either.

    Cheers,
    D-

    Reply
  4. Dorian says

    January 16, 2014 at 1:00 pm

    (hmm, looks like part of my previous message disappeared… please let me know if I could send you a PM with the rest of the code that isn’t visible right now)

    Reply
    • Jesin A says

      January 17, 2014 at 2:49 pm

      Dorian,

      The code in this article used curly quotes, I’ve fixed it now. Use the following code

      <?php
      if(is_category( 'fairytales' )) {
      $pagelink=get_page_link (get_page_by_title( 'fairytales' ));
      header("Location: $pagelink",TRUE,301);
      }
      if(is_category( 'club' )) {
      $pagelink=get_page_link (get_page_by_title( 'club' ));
      header("Location: $pagelink",TRUE,301);
      }
      elseif(is_category( 'space' )) {
      $pagelink=get_page_link (get_page_by_title( 'space' ));
      header("Location: $pagelink",TRUE,301);
      } ?>
      Reply
      • Dorian says

        January 18, 2014 at 4:31 pm

        Hi Jesin,
        Thanks for your reply, but I don’t quite see the difference between the new code you suggest and the one I tried already?
        Cheers,

        Reply
        • Jesin A says

          January 18, 2014 at 4:40 pm

          Look at the quotes and you’ll find the different.

          Earlier it was like ‘ ’“ ”

          Now it is ' ' " "

  5. Dorian says

    January 21, 2014 at 7:23 pm

    Oh yes, curly quotes — my tired eyes read “curly brackets,” sorry.
    Thanks for your help!

    Reply
  6. IHO says

    February 6, 2014 at 7:59 am

    That is awesome !! … Thank you very very very very very much, you just saved my life.

    Reply
  7. hulahop says

    August 30, 2014 at 5:38 am

    Thanks very much for the amazing explanation, it solved a big development problem I had and really spent such a long time looking for a solution.

    Found that if the slug has a category name with hyphens sometimes the get_page_by_title() function doesn’t work well, so you better have to refer to page by its unique id

    if(is_category( 'rock-and-roll' )) {
    $pagelink=get_page_link(id);
    header("Location: $pagelink",TRUE,301);
    }

    Hope that helps someone else 😀

    Reply
  8. Daniel says

    July 11, 2017 at 12:15 pm

    I want to add a link in my Homepage to redirect to a specific category archive.
    What are the step to takes and it is very urgent for me………
    Thanks.

    Reply
    • Jesin A says

      July 11, 2017 at 11:14 pm

      Try this code:

      function home_to_category_redirect() {
      	$category_id = get_cat_ID( 'Category Name' );
      	$category_link = get_category_link( $category_id );
      	exit( wp_redirect( $category_link ) );
      }
      
      add_action( 'template_redirect', 'home_to_category_redirect' );
      Reply
  9. Lindsay Doyle says

    November 8, 2017 at 9:19 am

    I have made pages (first link) and they have a contact form and that works great but the images for all of the pages made archives scroll page of all of the images. Is it possible to link the img in the archive page to link back to the original page published? Both pages are below, first id the original page with image and below that is the image that does not go anywhere and I would like to figure out how to link back to the original page. Is it possible to do this for all? I have hundreds that need redirected back to the original page

    https://flukemeterrepair.com/fluke-435-repair-services/
    https://flukemeterrepair.com/product/fluke-435-ii-repair/

    Reply
    • Jesin A says

      November 8, 2017 at 5:23 pm

      Hi Lindsay,

      If you use Yoast’s WordPress SEO plugin it has an option to redirect attachment pages to posts – https://kb.yoast.com/kb/redirect-image-attachment-urls/

      Reply
  10. Antonino says

    December 8, 2017 at 11:11 pm

    Hello, I want to redirect my latest posts archives pages (blog/page/1, blog/page/2…) to: blog/myposts/page/1 …. and so on.
    Is it possibile via .htaccess or other wp files?

    Thanks a lot!
    Antonino

    Reply
  11. Antonino says

    December 8, 2017 at 11:25 pm

    Hello, I found this, and seems to work:
    RedirectMatch 301 ^/blog/page/(.*)$ http://example.com/blog/myposts/page/$1

    Do you have any other suggestions? Do you think that one above has disadvantages (SEO, or other)?
    Thanks!

    Reply
    • Jesin A says

      December 10, 2017 at 12:34 am

      Hi Antonino,

      You could use this method to rename the pagination_base permalink.

      Reply
  12. Antonino says

    December 12, 2017 at 3:29 am

    Ok, thanks, I’ll try.
    Antonino

    Reply
  13. Alex says

    December 19, 2017 at 6:33 am

    Hi Jesin,

    I would like to redirect the category ‘Vacatures’ on page: http://werkenindeoffshore.nl/engineer-offshore/ to : http://werkenindeoffshore.nl/on-offshore-vacatures/

    but i doesnt seem to work. i tried all the things you said even tried with the page id number but it doesn’t seem to work.

    I’m working with multisite, i don’t know if thats a problem?

    Kindly,
    Alex

    Reply
    • Jesin A says

      December 19, 2017 at 6:19 pm

      Hi Alex,

      Create a file in the mu-plugins folder and place the following code:

      function my_category_redirect() {
      	if ( is_category( 'Vacatures' ) ) {
      		wp_redirect( get_permalink( 125 ) );
      		die;
      	}
      }
      
      add_action( 'template_redirect', 'my_category_redirect' );
      Reply
      • Alex says

        December 21, 2017 at 4:08 am

        Thanks for your reply!
        I created a mu-plugin folder in wp-content and placed a file with your code in it, but it didn’t work 🙁

        But i found a plugin that does the thrick, plus it’ s very simple. It’s called ‘REDIRECTION’

        https://wordpress.org/plugins/redirection/

        Regards

        Reply
  14. Ian S says

    February 15, 2024 at 8:05 pm

    I need to redirect all terms from a taxonomy ‘vendor-category’ to a page with a query.

    So I need to change: /vendor-category/meat-dairy-eggs/
    to: /vendors/?_filter_vendors_category=meat-dairy-eggs

    Reply
    • Jesin A says

      February 15, 2024 at 9:38 pm

      Add the following to the top of your .htaccess file:

      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^vendor\-category/(.+)/$ /vendors/?_filter_vendors_category=$1 [R=301,L]
      </IfModule>
      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.