I have a Drupal website which contains the below default mod_rewrite rules

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteCond %{REQUEST_URI} !=/server-status
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

That means that requests that match an existing file or directory will be served directly instead of triggering the index.php script.

The problem is that requests to non-existing files do not match the above rules and get redirected to index.php which bootstrap the entire Drupal application and unnecessarily load the website.

In order to bypass Drupal for missing files I have created the following rules:

  RewriteCond %{REQUEST_URI} (.jpg|.jpeg|.css|.js|.png)$
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule .* - [L,R=404]

This works, but the problem is that the 404 redirects relies on ErrorDocument 404 /index.php which ends up bootstrapping Drupal.

I need to keep ErrorDocument 404 /index.php because Drupal is used to serve useful 404 pages for dynamic content. I tried to define several ErrorDocument 404 (1 before my rules and 1 before the Drupal rules) but only the last one applies.

Q: How can I serve an empty 404 page for requests to non-existing files without changing the ErrorDocument 404 directive?


I think this is what you want: Emulating ErrorDocuments with Mod_Rewrite

  • While this may answer the question, it is better to include the relevant parts of the solution in your answer than relying on links to an offsite resource. – MT0 Dec 12 '17 at 11:54

Use a different HTTP status code instead?

Say, 403 (Forbidden) - what does it matter what the (malicious) client sees?

  • 1
    because I use Google webmaster tools to monitor broken pages on my website and I would like missing files to be flagged as 404 and not 403. – Max Mar 20 '12 at 11:49

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.