Setting custom 404 error in a single subdirectory (apache and .htaccess)

I have a simple wordpress page with some news. In root directory I’ve set .htaccess with rules:

RewriteBase /news/
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]

As you can see it’s set that if client requests a file or dir that doesn’t exist he’s redirected to /news/index.php.

Read More

Now, I would like to set that if he calls for non existing graphic file from /news/wp-content/uploads/ subdirectory he should get a custom graphic instead (let’s say /news/wp-content/uploads/noimage.png). For this I modified .htaccess:

RewriteBase /news/wp-content/upload/
RewriteCond %{REQUEST_FILENAME !-f
RewriteRule . /news/wp-content/upload/notFound.png

RewriteBase /news/
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]

But now when I try to load page (even index page) I get internal server error. What’s wrong here?

Related posts

Leave a Reply

2 comments

  1. Your first rule is actually hijacking all the legit WP requests since those are never for a valid file. Replace your complete code with this:

    if there is no .htaccess in wp-contnent directory:

    Options +FollowSymLinks -MultiViews
    
    RewriteEngine On
    RewriteBase /news/
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(wp-content/uploads)/.+$ $1/notFound.png [L,NC]
    
    RewriteRule ^index.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    

    if there is .htaccess in wp-contnent directory then use this code on top of wp-contnent/.htaccess file:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^uploads/.+$ /news/wp-content/uploads/notFound.png [L,NC]
    
  2. Instead of fiddling with the RewriteBase, why don’t you try this:

    RewriteCond %{REQUEST_URI} ^/news/wp-content/upload/ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . /news/wp-content/upload/notFound.png [L]