Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to throw 404 Errors when URL contains a certain string - Wordpress

I am managing a wordpress blog and want to throw a 404 error whenever the url contains a string pattern (example: if the url contains "thisisnotwanted"). I was thinking I will be able to add something to the htaccess file like: Redirect "thisisnotwanted" 404

Can someone help? I just don't want Google to index pages with this parameter.

like image 541
Sameer Avatar asked Nov 27 '25 03:11

Sameer


2 Answers

If you want to disallow Google from indexing pages, you should add a robots.txt file to the root folder of your website.

You could then put something like this in the file:

User-agent: *
Disallow: /thisisnotwanted

I assume you want to disallow the page from all search engines, but if you only want to disallow Google, the you should change the first line to User-agent: Google.

You can tell Google explicitly to remove the links using Webmaster tools. It could take a few days before Google will accept your request and remove the pages from their index.

For more information, please visit this website:
The Web Robots Pages

like image 64
Michiel Pater Avatar answered Nov 29 '25 16:11

Michiel Pater


This could be achived using robots.txt but since you're asking how to throw the 404 page manualy here it is :

<?php
if ( preg_match('/thisisnotwanted/i',$_SERVER["REQUEST_URI"]) ) {
    header("HTTP/1.0 404 Not Found - Archive Empty");
    require TEMPLATEPATH.'/404.php';
    exit;
}
get_header();
?>

This bit of code is just an example on how you can display a 404 page, and it shouldn't be used in "production", instead use robots.txt as Michiel Pater sugested .

like image 33
Poelinca Dorin Avatar answered Nov 29 '25 15:11

Poelinca Dorin