By "copy-pasting" from the web without really understanding what's going on, I made my .htaccess file like this :
Options +FollowSymLinks -MultiViews -Indexes
ErrorDocument 404 /index.php?notfound=1 # (a)
FallbackResource /index.php?notfound=1 # (b)
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [NC,QSA,L] # (c)
I feel that (a) and (b) directives are not useful because of (c)... am I right ?
What's the real difference between those 3 directives ?
They all do more or less the same thing here.
ErrorDocument 404andFallbackResourceboth set the given script as handler for anything that would otherwise cause a 404, because the requested URI does not map onto any existing file or folder.I think the main difference here would be, that with
ErrorDocument 404you’d still get an entry in the server’s error log each time, whereas withFallbackResourceit is considered implied that this is meant to be a front controller, so that won’t pollute your logs. If you needed any info about what actual 404s occured - then you would have to log them yourself from within your system, after it has determined that it actually did not find any content to serve under the requested URI.The Rewrite version is basically just the “old school” version of that, the documentation for
FallbackResourceexplicitly mentions that, https://httpd.apache.org/docs/2.4/mod/mod_dir.html#fallbackresource:So with either of the first two, you don’t even need mod_rewrite to be available (which it sometimes might not be, on cheap shared hosting or something.)
The Rewrite version as shown above, would pass the originally requested URI under the parameter name
url, so you have access to it using$_GET['url']. With either of the first two, it does not get explicitly passed in the new internally rewritten URL, so you have to access it from the server variables, as explained there a couple of lines further below,Using mod_rewrite for things like this still has its uses, if you don’t want to rewrite all requests to the same script, or you don’t want to do the parameter parsing in PHP - like you might want to rewrite
products/footoproducts.php?product=fooandprofile/usernametoprofile.php?user=username.ErrorDocument 404andFallbackResourcewon’t allow for that in particular, mod_rewrite with different sets of rules that match these path segments specifically, could do that however.I guess using
FallbackResourcewould be considered state-of-the-art these days by most, and handling the parsing of the URL and deciding what needs to happen next based on that, is usually considered to be more logically placed in the scripting part of a site / application, rather than having this in a separate location, like .htaccess.