Giter Site home page Giter Site logo

vr51 / fix-missing-files Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 1.0 30 KB

Serve replacement files when requests are made for non existent documents.

Home Page: https://journalxtra.com/web-development/fix-missing-file-404-requests/

License: GNU General Public License v3.0

ApacheConf 99.31% CSS 0.14% HTML 0.28% JavaScript 0.14% PHP 0.14%
apache 404 htaccess

fix-missing-files's Introduction

Fix-Missing-Files v0.0.2

Serve replacement files when requests are made for non existent documents i.e. fix 404 errors with a one-size-fits-all patch.

Notes

  • This solution for fixing 404 errors is intended to be used as a quick-fix when other solutions are impractical to apply.
  • This solution is not intended to be used to serve 404 error pages though it can be used this way if necessary.

Instructions

  • Download the directory called 'missing'.
  • Upload the 'missing' directory to your site's document root (normally under public_html).
  • Unpack the uploaded package.
  • Make sure 'missing' is directly under public_html
  • Copy the .htaccess directives in the 'missing' directory to the .htaccess file immediately under public_html.

For reference

The .htaccess directives to be added to the root .htaccess file are:

## Fix missing image and file requests
## Version 0.0.2
## Change each of the {REQUEST_URI} lines in each directive set to specify the missing file 404 requests to check against.
## The replacement file(s) to serve should go into a server directory called "missing" located under public_html/
## Image formats go into the Image Set. No file required to be added to the 'missing' directory
## Other formats go into the File Set. Place an empty text file with correct extension into the 'missing' directory
## See https://journalxtra.com/web-development/fix-missing-file-404-requests/
 
## Image Set: Fix Bad Image Requests
RewriteCond %{REQUEST_FILENAME} !-f
## Ignore any real files (404s will not occur if the file actually exists)
## Add image file types to check against here
RewriteCond %{REQUEST_URI} .(gif|jpe?g|png|bmp|tiff|xcf|psd|ico)$ [NC]
## Specify the file to serve instead
RewriteRule .* /missing/missing.gif [L]
 
## File Set: Fix Bad File Requests
## Ignore specific virtual file(s)
RewriteCond %{REQUEST_URI} !^/robots.txt/?$ [NC]
## Ignore any real files (404s will not occur if the file actually exists)
RewriteCond %{REQUEST_FILENAME} !-f
## Add file types to check here.
## Put a replacement document in the "missing" directory for each specified file type.
## The replacement document can be an empty text file but with the same extension type as the file it is served in place of.
RewriteCond %{REQUEST_URI} .(css|html?|js|php|shtml|svg|txt)$ [NC]
RewriteRule .* /missing/missing.%1 [L]

Files in the Missing directory

These are the files in the 'missing' directory. These are served when 404s are encountered. These are empty files. If you wish you can replace them with non-empty files to serve when missing files are encounted but I advise against it.

  • missing.gif (1px square)
  • missing.css (empty text file)
  • missing.htm (empty text file)
  • missing.html (empty text file)
  • missing.js (empty text file)
  • missing.php (empty text file)
  • missing.shtml (empty text file)
  • missing.svg (empty text file)
  • missing.txt (empty text file)

Warnings and Notes

If you use a CMS that creates virtual files be careful not to add those virtual file formats to the list of formats to check against or at least whitelist specific virtual files with a directive instructing the server to ignore the files (see comments within the File Set directives for an example of whitelisting).

WordPress Virtual File Formats

These are virtual files and file formats encountered by me:

  • xml (created by Jetpack and SEO plugins)
  • robots.txt (created by SEO plugins)

URL Slugs

If your URLs end with .html or another of the extensions checked in the File Set you will need to remove the extension from File Set.

For example, some WordPress sites use .html at the end of their permalinks. Usually a hangover from a migrated site. The rewrite condition in the File Set would be as follows for sites with such URL slugs:

RewriteCond %{REQUEST_URI} .(css|js|php|shtml|svg|txt)$ [NC]

Changelog

0.0.2 - 13th March 2017

  • Minor readme.md changes
  • Moved svg to File Set from Image Set

0.0.1 - 10th March 2017

  • First public release

More info

Visit https://journalxtra.com/web-development/fix-missing-file-404-requests/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.