Really stumped, because form and syntax seem fine.
RewriteCond for REQUEST_URI is not matching the explicit path and filename. When isolated, RewriteCond for REQUEST_FI
I was unable to determine why server configuration or site code was forcing '410 Gone' response directive in htaccess to be overridden with a 404 response, so had to do something like this to tell googlebot to stop hunting for CSS/JS files that get purged periodically (and renamed when regenerated).
in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule v_(.*)_(.*)$ /410response.php [L]
in 410response.php placed in root:
<?php header($_SERVER['SERVER_PROTOCOL'].' 410 Gone');
UPDATE I
The 404 response when attempting to use htaccess for the 410 directive was being forced by the server, because of server apparently having a custom 410 document, that apparently routed to 404. Adding a directive to prevent that then properly allowed use of htaccess to return 410 for pattern matches in RewriteRule. (I thought that I had already checked yesterday to see if this would work, since @MrWhite said in his answer above to control for server possibly having a custom 410; today when making this check, it did work and indicate that server 410-to-404 redirection was overridding my 410 directive.)
ErrorDocument 410 default
RewriteRule test\.txt$ - [NC,R=410]
MrWhite! I located this solution in one of your posts on Stack Exchange.
This isn't really a solid answer, more of a things to try to help debug this and to quash some myths...
I have verified using
phpinfo()
thatREQUEST_URI
contains the leading slash
Yes, the REQUEST_URI
Apache server variable does indeed contain the leading slash. It contains the full URL-path.
However, the REQUEST_URI
Apache server variable is not necessarily the same as the $_SERVER['REQUEST_URI']
PHP superglobal - in fact, they aren't really the same thing at all. There are some significant differences between these variables (in some ways it's perhaps a bit unfortunate they share the same name). Notably, the PHP superglobal contains the initial URL from the request and includes the query string (if any) and is not %-decoded. Whereas the Apache server variable of the same name contains the rewritten URL (not necessarily the requested URL) and does not contain the query string and is %-decoded.
So, that's why I was asking whether you have other mod_rewrite directives. You could very well have had a conflict. If another directive rewrites the URL, then the condition will never match (despite the PHP superglobal suggesting that it should).
It seemed that if I put this at the top, the Last flag would end processing for that trip through, return the 410
This directive should certainly go at the top of the .htaccess
file, to avoid the URL being rewritten earlier. The L
flag is actually superfluous when used with a R=410
(anything other than a 3xx
) - it is implied in this case.
Then I change the result to be "throw a 410" and it throws a 404.
That can certainly be caused by a server-side override. But you are able to throw a 410 in other situations, so that would seem to rule that out. However, you can reset the error document in .htaccess
if in doubt (unless you are already using a custom error document):
ErrorDocument 410 default
RewriteCond %{REQUEST_URI} ^/dir1/dir2/dir3/v_9991_0726dd5b5e8dd67a214c0c243436d131_all\.css$ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ - [R=410,L]
Whilst this doesn't really make a difference to how the rule behaves, you don't need the first RewriteCond
directive that checks against the REQUEST_URI
. You should be doing this check in the RewriteRule
pattern instead (which will be more efficient, since this is processed first). For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^dir1/dir2/dir3/v_9991_0726dd5b5e8dd67a214c0c243436d131_all\.css$ - [NC,R=410]
The NC
flag should be superfluous.
Still, a conflict with existing directives is the most probable cause. Remove all other directives. Do you still see the same behaviour?
You can test the value of the REQUEST_URI
server variable. You could either issue a redirect and pass the REQUEST_URI
as a URL parameter, or set environment variables (but you will need to look out for REDIRECT_<var>
for each rewrite).
For example, at the top of your .htaccess
(or wherever you are trying this):
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ /test.php?var=%{REQUEST_URI} [NE,R,L]
Created a dummy test.php
file to avoid an internal subrequest to an error document.