David Allen
David Allen

Reputation: 1163

Override 404 page for wordpress

We have a database of hundreds urls which we want to show similar content for. But we don't want to make hundreds pages for in wordpress.

Is there a way I can override/suppress wordpress 404 page to show the content from a page (already created) on all url in this database.

All I need is the code to do this to one URL and I'll be-able to do the rest.

Thanks

Upvotes: 1

Views: 2365

Answers (3)

dciso
dciso

Reputation: 1264

Within the theme folder use 404.php

This way you could find the URL that was called and customize the page a little.

Hope this helps, Jason

[Update]

I don't see why a plugin is needed. In the 404.php template you would have something like so:

<?php 

get_header();


//Get called URL 
$url = $_SERVER['REQUEST_URI'];

//Some function to check database to see if this URL should exist
if(checkIfPageExists($url)) {
   //Page Code Here
} else {
   //404 Code Here
}


get_footer();

?>

[Update 2]

Fixing the status code is easy:

<?php 

//Get called URL 
$url = $_SERVER['REQUEST_URI'];

//Some function to check database to see if this URL should exist
if(checkIfPageExists($url)) {
   //Set 200 Status
   status_header( "200" );
   //Get Header
   get_header();
   //Page Code Here
} else {
   //Set 404 Status
   status_header( "404" );
   //Get Header
   get_header();
   //404 Code Here
}


get_footer();

?>

Upvotes: 1

Nick
Nick

Reputation: 1167

You could use a 301 redirect in your htaccess file

RewriteRule ^your-db-url.html /your-redirect-page.html [L,R=301]

You may also be able to write a script that could list out all of your urls in this format and copy and paste this into your .htaccess file above your wordpress rewrite rules.

Upvotes: 0

Cogicero
Cogicero

Reputation: 1524

You can use htaccess. Enter the following at the top of the htaccess file in your wordpress installation. 404page.html is the error page you created.

ErrorDocument 404 /404page.html

Hope that helps.

Upvotes: 0

Related Questions