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

By : Sameer
Source: Stackoverflow.com

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.

By : Sameer


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');
By : Danial

Using mod_rewrite, it would be something like

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

in a .htaccess

By : huff

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

This video can help you solving your question :)
By: admin