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.
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.
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.
mary says
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
Jesin A says
Hi Mary,
This is possible using the is_tag() function. Pass the tag slug as an argument.
To redirect many tags to the same URL use the OR logical operator.
Tushar Sirwani says
Thanks alot Jesin A it worked!
mary says
It worked! Thank you soooooooooooooooo very much!
Jesin A says
Great to hear that!!!
Dorian says
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-
Dorian says
(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)
Jesin A says
Dorian,
The code in this article used curly quotes, I’ve fixed it now. Use the following code
Dorian says
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,
Jesin A says
Look at the quotes and you’ll find the different.
Earlier it was like
‘ ’“ ”
Now it is
' ' " "
Dorian says
Oh yes, curly quotes — my tired eyes read “curly brackets,” sorry.
Thanks for your help!
IHO says
That is awesome !! … Thank you very very very very very much, you just saved my life.
hulahop says
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
Hope that helps someone else 😀
Daniel says
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.
Jesin A says
Try this code:
Lindsay Doyle says
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/
Jesin A says
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/
Antonino says
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
Antonino says
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!
Jesin A says
Hi Antonino,
You could use this method to rename the pagination_base permalink.
Antonino says
Ok, thanks, I’ll try.
Antonino
Alex says
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
Jesin A says
Hi Alex,
Create a file in the mu-plugins folder and place the following code:
Alex says
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
Ian S says
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
Jesin A says
Add the following to the top of your .htaccess file: