2

Is there a way to handle base directories for taxonomies that result in a 404. For example my URLs are:

http://skipology.com/category/...
http://skipology.com/app/...

but the taxonomies / base directories above result in 404. Can they be redirected to a taxonomy index page (which I may need to create) for example without impacting the child directory (post) or is there a better option. If so how? I suspect the answer lies in redirects but I'm not confident.

3 Answers 3

0

You can manually create pages named category and app in admin under the Pages menu, and use a custom page template for each to list out taxonomy terms or whatever you need.

EDIT - 301 redirect a request that matches the pagename rewrite to another page:

function wpa_parse_query( $query ){
    if( isset( $query->query_vars['pagename'] )
        && 'apps' == $query->query_vars['pagename'] ){
            wp_redirect( home_url( '/other-page/' ), 301 );
            exit;
    }
}
add_action( 'parse_query', 'wpa_parse_query' );
9
  • Thank you that makes sense. As a follow up, if I already have a suitable page for 'apps' put can't change the URL could I create a new 'apps' page which is basically blank and 301 redirect it to the existing page (via Yoast I guess). If I did would that break the child directories beneath 'apps'?
    – Paul Brown
    Dec 9, 2013 at 23:39
  • that would probably work.
    – Milo
    Dec 9, 2013 at 23:56
  • I'll be honest I'm a bit worried that it would propagate to the child directories (i.e. posts) and break my site. I don't think I'll risk a redirect unless anyone has any experience of it. I'll probably just create a page with a link.
    – Paul Brown
    Dec 10, 2013 at 13:53
  • it's pretty straightforward, example above, tested and working. the page you're redirecting from doesn't even have to exist, the request just has to be for the pagename. it's not possible for this to match anything except apps explicitly.
    – Milo
    Dec 10, 2013 at 16:03
  • Oh wow so this is a 301 redirect that sits in functions.php rather than editing htaccess directly? Is that correct? Thanks Milo.
    – Paul Brown
    Dec 10, 2013 at 16:08
2

The answer by Milo above seems to potentially be a bit off the mark, because it doesn't result in http://skipology.com/category/ being an actual page, but instead it just redirects to another page. I don't know for sure so I'm just adding mine here as an alternative.

If you are ok with a little bit of manual setup for this, this would be the simplest way to do this.

  • Create a page and give it the slug 'category'
  • Create a page template and select if for the page you just created.

The manual part of this is that you will have to create a separate page for each taxonomy. Each page just needs to use the same template, as long as you make the template correctly. You should be able to just detect the taxonomy dynamically and just use the same template on each one. The upside of this is that you don't need to mess with any rewrite rules or manipulate the template hierarchy. You also make /category/ into an actual page rather than just redirecting to a different URL.

Alternatively, if you want to make this completely automatic as if it were part of the native Template Hierarchy, you will need to add a rewrite rule for each taxonomy to catch those URI's, then filter the template hierarchy to direct WordPress to the correct template. Take a look at this old answer of mine explaining how to filter the template hierarchy in different ways.

It seems to me that if you go the route the rewrite rules, you'll need to also add a custom query var. If this is the way you want to go, please let me know in a comment and I will update the answer with more details. I think the first method should suit you fine though.

5
  • Hi Eddie, for all taxonomies except one I do need to create a new page and for me that would be skipology.com/category and skipology.com/genre - so 2 new pages with slug category and genre. I guess having read your solution my only question would be do I need to create a new template or can I just create a page and use an existing template to add whatever content I need. Possibly I missed the point of your suggestion re new templates?
    – Paul Brown
    Dec 11, 2013 at 10:48
  • 1
    this is basically a more detailed explanation of the first sentence of my answer. the rewrite rules thing was a separate follow-up question for redirecting an existing page. upvote though, for the better explanation!
    – Milo
    Dec 11, 2013 at 16:18
  • Just to close this out (I think). I ended up with 3 different solutions. I guess if I were starting a blog from scratch I'd set it differently: 1. I used a 301 redirect to point my tag base (which is renamed app) to an existing page. 2. I created a new page for a custom taxonomy at the taxonomy base (genre). 3. Categories are main menu items so I just 301 redirect that to the home page. The only issue then was that I use breadcrumbs so I had to write some functions to make the breadcrumbs mirror the structure. Thanks for all help.
    – Paul Brown
    Dec 12, 2013 at 11:43
  • 1
    @PaulBrown - in response to you questions regarding the new templates. If you have a template that already does what you need that page to do, then all you would need to do is add the proper comment header to it, so that it registers as a "Page Template" - then your done, - you can use that template for any given Page.
    – eddiemoya
    Dec 13, 2013 at 23:52
  • Thanks Eddie, I did end up creating a new template - the coding isn't pretty but the the results are what I had in mind :)
    – Paul Brown
    Dec 14, 2013 at 0:45
0

In the end I've found and tested some code that works in .htaccess...

RedirectMatch 301 /app/?$ http://skipology.com/iphoneography-app-index/

Just need to create something to redirect the other taxonomies to now.

4
  • Was your original goal to use a get a 301 redirect out of this? From your question is seems like you would have wanted example.com/category to be an actual page, not just a redirect. I will post an alternative answer in case thats actually what you wanted.
    – eddiemoya
    Dec 11, 2013 at 5:15
  • Hi Eddie, for all of my taxonomy bases I want to remove the 404 and create a page but for one specific taxonomy 'app' I already have a page created which is perfect. As it is longstanding I'd like to leave it where it is so a redirect for 'app' to the existing page seems the best solution. For the others I'll need to create new pages / solutions. I couldn't get the redirect to work from Milo's code for some reason but the 301 I posted works fine as far as I can see.
    – Paul Brown
    Dec 11, 2013 at 10:43
  • why dont you just change the slug for 'iphoneography-app-index' to 'app'?
    – eddiemoya
    Dec 13, 2013 at 23:49
  • I'm assuming Wordpress takes care of the 301 for the search engines if I did that (if not I could add a 301 to the new slug anyway). The only reason I don't is vanity - the page has a good number of social media likes which as far as I'm aware don't get ported across with a redirect. It's a minor issue for a small blog like mine but even so I understand these things have some sort of impact on search ranking. I did think about it and that was my thought process.
    – Paul Brown
    Dec 14, 2013 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.