I have a unique kind of question that I’ve been wrapping my brain around. Google is penalizing us with duplicate content because you can access our single blog posts both by going to:
http://www.domain.com/blog/the-post-slug
as well as :
http://www.domain.com/the-post-slug
The option we really want is:
http://www.domain.com/blog/the-post-slug
and if a user or googlebot happens to goto:
http://www.domain.com/the-post-slug
redirect them to:
http://www.domain.com/blog/the-post-slug
I know this is a particular situation, but we have many custom post types in our system.
So our structure looks like this.
http://www.domain.com/food/pizza
http://www.domain.com/ice-cream/strawberry
http://www.domain.com/fireworks/bottle-rockets
Ideally, what I would like to do is find some kind of filter that logically checks if the post_type is post and redirect:
http://www.domain.com/the-post-slug
to
http://www.domain.com/blog/post-slug
I tried modifying the register_post_type for the post object, and adding with_front => false and adding blog to the slug, but that doesn’t work.
I was able to add /blog/%postname%/ to the permalink structure, and I was successfully able to have a decent structure going that was working, but I got stuck on the taxonomies. When I went to a taxonomy page, I had to goto:
http://www.domain.com/blog/the-taxonomy/flavors
and:
http://www.domain.com/the-taxonomy/flavors
was giving me a 404. So even if I can find a way to make the taxonomies work to where I can do:
http://www.domain.com/the-taxonomy/flavors
that would be awesome! As another side note, I also added with_front to the register_taxonomy, but that was not doing anything either. I even hard flushed the rewrites on all the changes I did.
I know this is a lot, but my brain cannot go any further.. lol
TIA!
Any insight on this?
First of all set your permalink structure to
/%postname%
.After that add a rewrite rule to rewrite all request like
example.com/blog/postname
to the appropriate query:Now you have to prevent the duplicate url, sending a 301 redirect when
example.com/postname
is requested:Finally you have to create permalink for your post with ‘blog/’ in front:
Last thing to is flush rewrite rules and… test.
Edit
As @Milo pointed out in comments, all of this can be avoided by setting permalink structure to
/blog/%postname%
and then use the param'rewrite'
having'with_front'
tofalse
for all CPTs and all taxonomies.The problem is that most times, in a average WP install with a lot of CPT and taxonomies, there are chances that some of them are registred via 3rd party plugins…
in that case if that plugins register the CPTs/taxonomies not using
'with_front' => false
it’s a problem and the solution posted above is the last chance.In addiction I have to say that standard categories and tags are registered with ‘with_front’ = true, so using this alternative method also standard category and tag must be re-registered with
with_front
param setted tofalse
.