0

I want to hide, let's say, example.com/services/washing ('Services' is the this is a custom post type) from the search engine and show a 401 error page. How do I?

3 Answers 3

1

Instead of showing a 401, why not just add the URL to a robots.txt and the search engine will ignore it from indexing.

You could also manually add to the headers a meta robots to noindex the page.

You could run this via .htaccess to redirect a page to somewhere else (or show 401), but that would show for both users and search engines.

3
  • Can you write for me, what tag I have to put in the header?
    – Warda
    Commented Dec 21, 2022 at 13:53
  • 1
    <meta name="robots" content="noindex"> If you add that to the header section of any page, then the page will not be indexed by search engines. Commented Dec 21, 2022 at 17:17
  • thank you so much
    – Warda
    Commented Dec 21, 2022 at 22:55
1

You can do this with Yoast SEO plugin. After you install the plugin, go to:

Yoast SEO > Search Appearance > Content Types

Find your custom post type, then select "Off" on "Show [your_custom_post_type] in search results?". You can do this for each post type.

But I don't think it is recommanded to redirect to a 401 page. It could negatively impact your website's SEO. Otherwise, you can redirect to example.com.

Hope it helps!

3
  • I tried code and customizer permalink plugin, but it broke my all permalinks. So I revert it back. Yes, I'll try the Yoast SEO plugin. Thanks for the suggestion!
    – Warda
    Commented Dec 18, 2022 at 21:41
  • I want to redirect the custom post-type URL which wasn't in use. so if the user searches, it will show an error page.
    – Warda
    Commented Dec 18, 2022 at 23:27
  • You can do this with the "Redirection" plugin. Just add a simple redirection off the source URL to the target URL. Commented Dec 20, 2022 at 11:47
1

You can completely remove a custom post type slug from the url with the following code, and then you can redirect that particular url to another page. 401 is used for authorization, and if your site has any type of Captcha system, it will work as usual on child CPT url.

function hide_slug( $post_link, $post, $leavename ) {

if ( 'CPT_NAME' != $post->post_type || 'publish' != $post->post_status ) {
    return $post_link;
}

$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

return $post_link; }
add_filter( 'post_type_link', 'hide_slug', 10, 3 );
1
  • www.example.com/services/washing/ - I need to discourage 'services' from this link. no matter what I try it comes in search. I want to see services page by visitors
    – Warda
    Commented Dec 19, 2022 at 16:54

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