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.

Related posts

Leave a Reply

4 comments

  1. 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

  2. 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 .

  3. You could set a conditional statement in your theme file to redirect the viewer to the 404 page.

    using this code:

    $wp_url = $_SERVER["REQUEST_URI"] //from poelinca
    if(preg_match('/thisisnotwanted/',$wp_url)) header('location:/404page');
    
  4. Using mod_rewrite, it would be something like

    RewriteEngine on
    RewriteCond %{THE_REQUEST} thisisnotwanted[ /?].*HTTP/
    RewriteRule ^.*$ - [R=404,L]
    

    in a .htaccess