I apologize in advance for the likely misuse of coding terminology. I am not very skilled when it comes to this.

In a separate website, I have links which when clicked bring up a .pdf of images corresponding to the feature's unique id:
which results in something like example.com/Images/1234.pdf for feature 1234.

Now let's say someone clicks on the link for feature 1235, but it has no corresponding .pdf. The page currently redirects my generic 404 page I created in wordpress. How do I get it to direct to the specific page example.com/Images/nopdf.pdf for all non-existent pages under the subdomain /Images/? I appreciate in advance for the dumbed-down answers.


I'd suggest customizing your 404 template, within your theme. That way you can add a conditional that only sends people to the "not found PDF" if they were trying to access a PDF. If they were trying to access any other document type, like a webpage, that doesn't exist, they will still see your regular 404 page.

To set this up:

If you are not already using a custom theme or a child theme, set one up. That way when you update your theme, you won't lose your customizations.

Edit, or create, a file called "404.php" in your (child or custom) theme's root directory. The contents should be something like this:

// check the requested URL
// see if the URL contains the "Images" folder
preg_match('/Images/', $requestUrl, $matches);
// if the URL matches, it will be a non-empty array
if(count($matches) > 0) {
// if the URL didn't match, use the normal 404 template
} else {
    // your normal theme 404 code, starting with get_header();
    // ...
    // ...
    // don't forget the closing } at the end of the file

There is one other alternative if you don't want to edit theme files. You can create a plugin that does the same thing. Basically, you check if is_404(), then put in the same code - if the URL contains the Images folder, redirect to your pdf. You wouldn't need an "else" condition, you'd just let WP do its normal thing.

  • This is exactly the direction I want to go. Let me mess with it and see if I can get it to work.
    – Deacon
    Jun 22 '17 at 15:22
  • So I get redirected to my default 404 page with the warning: "Warning: preg_match() [function.preg-match]: Unknown modifier 'l' in /home/slvgis5/public_html/wp-content/themes/twentyeleven-child/404‌​.php on line 3" Line 3 reads: preg_match('/images/Alamosa_PC/Images/', $requestUrl, $matches);
    – Deacon
    Jun 22 '17 at 15:50
  • In PHP regex the slash has to be escaped. So if you're trying to match that folder you'd actually type it like this: preg_match('/\/images\/Alamosa_PC\/Images\//', $requestUrl, $matches);
    – WebElaine
    Jun 22 '17 at 16:27
  • I love you. You are the greatest!!!!!
    – Deacon
    Jun 22 '17 at 16:31

Welcome to wordpress.stackexchange.com.

How to create custom 401, 403 and 500 error pages?

Please have a look at above answer and see how it is done. In short, you need to direct the current 404 page to example.com/Images/nopdf.pdf by putting this in your .htaccess file.

ErrorDocument 404 example.com/Images/nopdf.pdf

I am not even sure if serving up a PDF is a good idea as an error document. Make a short PHP document, that, if must be, has the PDF inside. Also keep in mind that this file will be loaded whenever there is a 404 error on your site, not just when an image cannot be found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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