1

I am using the following .htaccess file, which works well, however, I want to also forward any attempts to access non-existent files in the root directory and in any subdirectory (whether the subdirectory exists or not) back to the main domain name.

I tried FallbackResource but that resulted in 500 series errors.

RewriteEngine On

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://xxxxxxxxx.xxxxx/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|bmp|zip|rar|mp3|flv|swf|png|css|pdf)$ -     [F]

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

ErrorDocument 500 /redirect
ErrorDocument 500 /redirect
ErrorDocument 500 /redirect
ErrorDocument 404 /error
ErrorDocument 401 /redirect
ErrorDocument 403 /redirect

Any help would be greatly appreciated.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
Sherif
  • 33
  • 2
  • what does the error log say? – danblack Sep 10 '18 at 23:39
  • 404 errors with entries for css, js and an image with a path with the random directory that doesn't exist. The paths to those includes are relative. That's why I'd rather just forward back to the main site. – Sherif Sep 11 '18 at 00:41
  • That sounds like the "access log", not the "error log"? You don't normally see 404 HTTP responses (which are not strictly server "errors") in the "error log". "The paths to those includes are relative. That's why I'd rather just forward back to the main site." - not sure what you mean by the first bit about relative paths? Can you give an example of the URL(s) being requested. – MrWhite Sep 11 '18 at 00:48

1 Answers1

0

FallbackResource results in the URL being internally rewritten to another file. However, I would have thought an external redirect would be preferable in this situation? Also, FallbackResource might conflict with your existing mod_rewrite directives. (However, it would have been useful to see your use of FallbackResource in-place, as it could have been your syntax that was at fault?)

Try something like the following, after your last mod_rewrite directive (before your ErrorDocument directives):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [QSD,R=302,L]

The above says... if the request does not map to a file and does not map to a directory then redirect to the document root (temporary 302). The QSD is required to remove any query string from the request.

Aside: This is not recommended from an SEO (or users) point of view, if that is a concern.


ErrorDocument 500 /redirect
ErrorDocument 500 /redirect
ErrorDocument 500 /redirect
ErrorDocument 404 /error
ErrorDocument 401 /redirect
ErrorDocument 403 /redirect

(Apart from unnecessarily defining the 500 error document more than once...) the document specified in the ErrorDocument directive should be a root-relative filesystem path. eg. /redirect.php (But that doesn't actually redirect does it?) Presumably you don't actually have a file called /redirect (that would require further rewriting).

MrWhite
  • 11,643
  • 4
  • 25
  • 40