Why was this -f flag returning true?Apache2 - mod_expire and mod_rewrite not working in httpd.conf - serving content from tomcatRedirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod_Rewrite Rules but Were Afraid to Ask.htaccess: Backreference in RewriteCond to check for file existence?htaccess rewrite?Apache RewriteRule causes 500 errorMoving WP blog to Tumblr but still use old WP server's images?Apache2 mod_rewrite failing for non-existent sub directoriesApache ModRewrite strip part from request_uri in rewriteConditionWhy doesn't this .htaccess file redirect properly?htacces RewriteRule does not trigger on every request

Placement of positioning lights on A320 winglets

How (un)safe is it to ride barefoot?

Nth term of Van Eck Sequence

Why do (or did, until very recently) aircraft transponders wait to be interrogated before broadcasting beacon signals?

Why didn't all the iron and heavier elements find their way to the center of the accretion disc in the early solar system?

Idiom for 'person who gets violent when drunk"

Is this Homebrew Eldritch Invocation, Accursed Memory, balanced?

How can powerful telekinesis avoid violating Newton's 3rd Law?

Why would a home insurer offer a discount based on credit score?

Course development: can I pay someone to make slides for the course?

What's the difference between DHCP and NAT? Are they mutually exclusive?

Is plausible to have subspecies with & without separate sexes?

If the pressure inside and outside a balloon balance, then why does air leave when it pops?

As easy as Three, Two, One... How fast can you go from Five to Four?

How can I find out about the game world without meta-influencing it?

Changing the PK column of a data extension without completely recreating it

Is the first of the 10 Commandments considered a mitzvah?

About the paper by Buekenhout, Delandtsheer, Doyen, Kleidman, Liebeck and Saxl

When editor does not respond to the request for withdrawal

Is fission/fusion to iron the most efficient way to convert mass to energy?

Why didn't the people of King's Landing riot when the Great Sept of Baelor was destroyed?

Content Editor Web Part - SharePoint Online?

Undocumented incompatibility between changes and siunitx?

Print "N NE E SE S SW W NW"



Why was this -f flag returning true?


Apache2 - mod_expire and mod_rewrite not working in httpd.conf - serving content from tomcatRedirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod_Rewrite Rules but Were Afraid to Ask.htaccess: Backreference in RewriteCond to check for file existence?htaccess rewrite?Apache RewriteRule causes 500 errorMoving WP blog to Tumblr but still use old WP server's images?Apache2 mod_rewrite failing for non-existent sub directoriesApache ModRewrite strip part from request_uri in rewriteConditionWhy doesn't this .htaccess file redirect properly?htacces RewriteRule does not trigger on every request






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I recently set up a rewrite to serve .webp files in place of .jpg files whenever possible. It seemed to be working correctly until today we noticed that a newly uploaded .jpg image was returning a 404 even though the image was present. I did some checking and found that the server was rewriting the .jpg URL to .webp, and then getting a 404 because the .webp hadn't been created yet.



I then moved the rule from vhost_ssl.conf over to .htaccess and did a bunch of tinkering until I found something that worked, then moved that solution back to vhost_ssl.conf. But I still don't understand why the original rewrite sometimes failed. Can anyone review and shed some light on the subject?



Original rewrite:



RewriteCond %HTTP_ACCEPT image/webp
RewriteCond %DOCUMENT_ROOT/%1.webp -f
RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]


Final rewrite:



RewriteCond %HTTP_ACCEPT image/webp
RewriteCond %REQUEST_URI (.+).(?:jpe?g|png)$
RewriteCond %DOCUMENT_ROOT/%1.webp -f
RewriteRule .* %1.webp [NC,T=image/webp,E=webp,L]


The only real difference between the two methods is that in the new method I check if the request URI matches jpeg/png before the rewrite condition that checks if the file exists. I think the new method is less efficient since the rewrite rule matches everything, but at least the condition above filters it, and it's in vhost_ssl.conf which is better than .htaccess.



Some other notes:



  • During testing, I changed the original rewrite to a 301 redirect so I could see the file it thought it found. Sure enough, the file was the same as the .jpg file, just with a .webp extension - correct path and all (even though the .webp didn't really exist).

  • Also during testing, I tried changing the rewrite condition to RewriteCond %DOCUMENT_ROOT/%1.webp_asdfasdfasf -fjust to verify that it would in fact fail, and it did. So it knew that the completely bogus file didn't exist, but it still thought the .webp file did exist.









share|improve this question




























    1















    I recently set up a rewrite to serve .webp files in place of .jpg files whenever possible. It seemed to be working correctly until today we noticed that a newly uploaded .jpg image was returning a 404 even though the image was present. I did some checking and found that the server was rewriting the .jpg URL to .webp, and then getting a 404 because the .webp hadn't been created yet.



    I then moved the rule from vhost_ssl.conf over to .htaccess and did a bunch of tinkering until I found something that worked, then moved that solution back to vhost_ssl.conf. But I still don't understand why the original rewrite sometimes failed. Can anyone review and shed some light on the subject?



    Original rewrite:



    RewriteCond %HTTP_ACCEPT image/webp
    RewriteCond %DOCUMENT_ROOT/%1.webp -f
    RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]


    Final rewrite:



    RewriteCond %HTTP_ACCEPT image/webp
    RewriteCond %REQUEST_URI (.+).(?:jpe?g|png)$
    RewriteCond %DOCUMENT_ROOT/%1.webp -f
    RewriteRule .* %1.webp [NC,T=image/webp,E=webp,L]


    The only real difference between the two methods is that in the new method I check if the request URI matches jpeg/png before the rewrite condition that checks if the file exists. I think the new method is less efficient since the rewrite rule matches everything, but at least the condition above filters it, and it's in vhost_ssl.conf which is better than .htaccess.



    Some other notes:



    • During testing, I changed the original rewrite to a 301 redirect so I could see the file it thought it found. Sure enough, the file was the same as the .jpg file, just with a .webp extension - correct path and all (even though the .webp didn't really exist).

    • Also during testing, I tried changing the rewrite condition to RewriteCond %DOCUMENT_ROOT/%1.webp_asdfasdfasf -fjust to verify that it would in fact fail, and it did. So it knew that the completely bogus file didn't exist, but it still thought the .webp file did exist.









    share|improve this question
























      1












      1








      1








      I recently set up a rewrite to serve .webp files in place of .jpg files whenever possible. It seemed to be working correctly until today we noticed that a newly uploaded .jpg image was returning a 404 even though the image was present. I did some checking and found that the server was rewriting the .jpg URL to .webp, and then getting a 404 because the .webp hadn't been created yet.



      I then moved the rule from vhost_ssl.conf over to .htaccess and did a bunch of tinkering until I found something that worked, then moved that solution back to vhost_ssl.conf. But I still don't understand why the original rewrite sometimes failed. Can anyone review and shed some light on the subject?



      Original rewrite:



      RewriteCond %HTTP_ACCEPT image/webp
      RewriteCond %DOCUMENT_ROOT/%1.webp -f
      RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]


      Final rewrite:



      RewriteCond %HTTP_ACCEPT image/webp
      RewriteCond %REQUEST_URI (.+).(?:jpe?g|png)$
      RewriteCond %DOCUMENT_ROOT/%1.webp -f
      RewriteRule .* %1.webp [NC,T=image/webp,E=webp,L]


      The only real difference between the two methods is that in the new method I check if the request URI matches jpeg/png before the rewrite condition that checks if the file exists. I think the new method is less efficient since the rewrite rule matches everything, but at least the condition above filters it, and it's in vhost_ssl.conf which is better than .htaccess.



      Some other notes:



      • During testing, I changed the original rewrite to a 301 redirect so I could see the file it thought it found. Sure enough, the file was the same as the .jpg file, just with a .webp extension - correct path and all (even though the .webp didn't really exist).

      • Also during testing, I tried changing the rewrite condition to RewriteCond %DOCUMENT_ROOT/%1.webp_asdfasdfasf -fjust to verify that it would in fact fail, and it did. So it knew that the completely bogus file didn't exist, but it still thought the .webp file did exist.









      share|improve this question














      I recently set up a rewrite to serve .webp files in place of .jpg files whenever possible. It seemed to be working correctly until today we noticed that a newly uploaded .jpg image was returning a 404 even though the image was present. I did some checking and found that the server was rewriting the .jpg URL to .webp, and then getting a 404 because the .webp hadn't been created yet.



      I then moved the rule from vhost_ssl.conf over to .htaccess and did a bunch of tinkering until I found something that worked, then moved that solution back to vhost_ssl.conf. But I still don't understand why the original rewrite sometimes failed. Can anyone review and shed some light on the subject?



      Original rewrite:



      RewriteCond %HTTP_ACCEPT image/webp
      RewriteCond %DOCUMENT_ROOT/%1.webp -f
      RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]


      Final rewrite:



      RewriteCond %HTTP_ACCEPT image/webp
      RewriteCond %REQUEST_URI (.+).(?:jpe?g|png)$
      RewriteCond %DOCUMENT_ROOT/%1.webp -f
      RewriteRule .* %1.webp [NC,T=image/webp,E=webp,L]


      The only real difference between the two methods is that in the new method I check if the request URI matches jpeg/png before the rewrite condition that checks if the file exists. I think the new method is less efficient since the rewrite rule matches everything, but at least the condition above filters it, and it's in vhost_ssl.conf which is better than .htaccess.



      Some other notes:



      • During testing, I changed the original rewrite to a 301 redirect so I could see the file it thought it found. Sure enough, the file was the same as the .jpg file, just with a .webp extension - correct path and all (even though the .webp didn't really exist).

      • Also during testing, I tried changing the rewrite condition to RewriteCond %DOCUMENT_ROOT/%1.webp_asdfasdfasf -fjust to verify that it would in fact fail, and it did. So it knew that the completely bogus file didn't exist, but it still thought the .webp file did exist.






      virtualhost .htaccess mod-rewrite httpd.conf






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 28 at 20:20









      Mike WillisMike Willis

      927




      927




















          1 Answer
          1






          active

          oldest

          votes


















          2














          tl;dr




          RewriteCond %DOCUMENT_ROOT/%1.webp -f



          It should be $1, not %1 in the RewriteCond TestString. Although it's unclear why this behaviour should have only recently changed (or maybe it hasn't?).







          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT/%1.webp -f
          RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]



          This should never have worked, unless you had a file called ".webp" (with no basename, starting with a dot) in the document root. If you did, then it would have always rewritten the jpeg/png URL to <whatever>.webp, whether it existed or not.



          So, this would seem to suggest you do have a file called .webp (no basename) in the document root? (And/or you are seeing a cached redirect for some requests? See below...)



          The %1 backreference in the 2nd RewriteCond TestString should be $1, not %1. %1 is a backreference to the first captured group in the last matched CondPattern - of which there is none, so %1 is always empty. So, it will always test the existence of /path/to/document-root/.webp. $1 on the other hand is a backreference to the first captured group in the RewriteRule pattern, ie. the filesystem path/basename, excluding the file extension.



          You have used $1 in the RewriteRule substitution - you should have used the same in the preceding condition.



          Note, that if these directives are in a Virtual Host context then you should either... remove the slash in the RewriteCond TestString, for example:



          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT$1.webp -f
          RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]


          OR, exclude the slash in the RewriteRule captured group. For example:



          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT/$1.webp -f
          RewriteRule ^/(.+).(?:jpe?g|png)$ /$1.webp [NC,T=image/webp,E=webp,L]


          Otherwise, you'll end up with a double slash in the TestString that the OS will need to resolve.




          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %REQUEST_URI (.+).(?:jpe?g|png)$
          RewriteCond %DOCUMENT_ROOT/%1.webp -f
          RewriteRule .* %1.webp [NC,T=image/webp,E=webp,L]



          This achieves the same as simply using $1 in the original rule block (as mentioned above). Curious that you changed the backreference in the substitution to %1, but knew to leave the backreference in the condition unchanged (or was this simply overlooked)?




          During testing, I changed the original rewrite to a 301 redirect ...




          Always test with 302 (temporary) redirects. 301s are cached persistently by the browser, so can cause caching issues later. (In fact, this can account for "intermittent" behaviour when the request hits the browser cache.)






          share|improve this answer




















          • 1





            Crap!! I do have a .webp file in the directory root, probably resulting from an automated script that creates the .webp files accidentally creating this as well. About the %1 backreference yesterday, I had the idea that %1 is for rewrite condition and $1 is for rules. I had the incorrect %1 originally because I thought that since it was being used as part of a rewrite condition, it should be %1. I have it working now, thanks!

            – Mike Willis
            May 29 at 16:08











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "2"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f969217%2fwhy-was-this-f-flag-returning-true%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          tl;dr




          RewriteCond %DOCUMENT_ROOT/%1.webp -f



          It should be $1, not %1 in the RewriteCond TestString. Although it's unclear why this behaviour should have only recently changed (or maybe it hasn't?).







          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT/%1.webp -f
          RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]



          This should never have worked, unless you had a file called ".webp" (with no basename, starting with a dot) in the document root. If you did, then it would have always rewritten the jpeg/png URL to <whatever>.webp, whether it existed or not.



          So, this would seem to suggest you do have a file called .webp (no basename) in the document root? (And/or you are seeing a cached redirect for some requests? See below...)



          The %1 backreference in the 2nd RewriteCond TestString should be $1, not %1. %1 is a backreference to the first captured group in the last matched CondPattern - of which there is none, so %1 is always empty. So, it will always test the existence of /path/to/document-root/.webp. $1 on the other hand is a backreference to the first captured group in the RewriteRule pattern, ie. the filesystem path/basename, excluding the file extension.



          You have used $1 in the RewriteRule substitution - you should have used the same in the preceding condition.



          Note, that if these directives are in a Virtual Host context then you should either... remove the slash in the RewriteCond TestString, for example:



          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT$1.webp -f
          RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]


          OR, exclude the slash in the RewriteRule captured group. For example:



          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT/$1.webp -f
          RewriteRule ^/(.+).(?:jpe?g|png)$ /$1.webp [NC,T=image/webp,E=webp,L]


          Otherwise, you'll end up with a double slash in the TestString that the OS will need to resolve.




          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %REQUEST_URI (.+).(?:jpe?g|png)$
          RewriteCond %DOCUMENT_ROOT/%1.webp -f
          RewriteRule .* %1.webp [NC,T=image/webp,E=webp,L]



          This achieves the same as simply using $1 in the original rule block (as mentioned above). Curious that you changed the backreference in the substitution to %1, but knew to leave the backreference in the condition unchanged (or was this simply overlooked)?




          During testing, I changed the original rewrite to a 301 redirect ...




          Always test with 302 (temporary) redirects. 301s are cached persistently by the browser, so can cause caching issues later. (In fact, this can account for "intermittent" behaviour when the request hits the browser cache.)






          share|improve this answer




















          • 1





            Crap!! I do have a .webp file in the directory root, probably resulting from an automated script that creates the .webp files accidentally creating this as well. About the %1 backreference yesterday, I had the idea that %1 is for rewrite condition and $1 is for rules. I had the incorrect %1 originally because I thought that since it was being used as part of a rewrite condition, it should be %1. I have it working now, thanks!

            – Mike Willis
            May 29 at 16:08















          2














          tl;dr




          RewriteCond %DOCUMENT_ROOT/%1.webp -f



          It should be $1, not %1 in the RewriteCond TestString. Although it's unclear why this behaviour should have only recently changed (or maybe it hasn't?).







          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT/%1.webp -f
          RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]



          This should never have worked, unless you had a file called ".webp" (with no basename, starting with a dot) in the document root. If you did, then it would have always rewritten the jpeg/png URL to <whatever>.webp, whether it existed or not.



          So, this would seem to suggest you do have a file called .webp (no basename) in the document root? (And/or you are seeing a cached redirect for some requests? See below...)



          The %1 backreference in the 2nd RewriteCond TestString should be $1, not %1. %1 is a backreference to the first captured group in the last matched CondPattern - of which there is none, so %1 is always empty. So, it will always test the existence of /path/to/document-root/.webp. $1 on the other hand is a backreference to the first captured group in the RewriteRule pattern, ie. the filesystem path/basename, excluding the file extension.



          You have used $1 in the RewriteRule substitution - you should have used the same in the preceding condition.



          Note, that if these directives are in a Virtual Host context then you should either... remove the slash in the RewriteCond TestString, for example:



          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT$1.webp -f
          RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]


          OR, exclude the slash in the RewriteRule captured group. For example:



          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT/$1.webp -f
          RewriteRule ^/(.+).(?:jpe?g|png)$ /$1.webp [NC,T=image/webp,E=webp,L]


          Otherwise, you'll end up with a double slash in the TestString that the OS will need to resolve.




          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %REQUEST_URI (.+).(?:jpe?g|png)$
          RewriteCond %DOCUMENT_ROOT/%1.webp -f
          RewriteRule .* %1.webp [NC,T=image/webp,E=webp,L]



          This achieves the same as simply using $1 in the original rule block (as mentioned above). Curious that you changed the backreference in the substitution to %1, but knew to leave the backreference in the condition unchanged (or was this simply overlooked)?




          During testing, I changed the original rewrite to a 301 redirect ...




          Always test with 302 (temporary) redirects. 301s are cached persistently by the browser, so can cause caching issues later. (In fact, this can account for "intermittent" behaviour when the request hits the browser cache.)






          share|improve this answer




















          • 1





            Crap!! I do have a .webp file in the directory root, probably resulting from an automated script that creates the .webp files accidentally creating this as well. About the %1 backreference yesterday, I had the idea that %1 is for rewrite condition and $1 is for rules. I had the incorrect %1 originally because I thought that since it was being used as part of a rewrite condition, it should be %1. I have it working now, thanks!

            – Mike Willis
            May 29 at 16:08













          2












          2








          2







          tl;dr




          RewriteCond %DOCUMENT_ROOT/%1.webp -f



          It should be $1, not %1 in the RewriteCond TestString. Although it's unclear why this behaviour should have only recently changed (or maybe it hasn't?).







          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT/%1.webp -f
          RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]



          This should never have worked, unless you had a file called ".webp" (with no basename, starting with a dot) in the document root. If you did, then it would have always rewritten the jpeg/png URL to <whatever>.webp, whether it existed or not.



          So, this would seem to suggest you do have a file called .webp (no basename) in the document root? (And/or you are seeing a cached redirect for some requests? See below...)



          The %1 backreference in the 2nd RewriteCond TestString should be $1, not %1. %1 is a backreference to the first captured group in the last matched CondPattern - of which there is none, so %1 is always empty. So, it will always test the existence of /path/to/document-root/.webp. $1 on the other hand is a backreference to the first captured group in the RewriteRule pattern, ie. the filesystem path/basename, excluding the file extension.



          You have used $1 in the RewriteRule substitution - you should have used the same in the preceding condition.



          Note, that if these directives are in a Virtual Host context then you should either... remove the slash in the RewriteCond TestString, for example:



          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT$1.webp -f
          RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]


          OR, exclude the slash in the RewriteRule captured group. For example:



          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT/$1.webp -f
          RewriteRule ^/(.+).(?:jpe?g|png)$ /$1.webp [NC,T=image/webp,E=webp,L]


          Otherwise, you'll end up with a double slash in the TestString that the OS will need to resolve.




          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %REQUEST_URI (.+).(?:jpe?g|png)$
          RewriteCond %DOCUMENT_ROOT/%1.webp -f
          RewriteRule .* %1.webp [NC,T=image/webp,E=webp,L]



          This achieves the same as simply using $1 in the original rule block (as mentioned above). Curious that you changed the backreference in the substitution to %1, but knew to leave the backreference in the condition unchanged (or was this simply overlooked)?




          During testing, I changed the original rewrite to a 301 redirect ...




          Always test with 302 (temporary) redirects. 301s are cached persistently by the browser, so can cause caching issues later. (In fact, this can account for "intermittent" behaviour when the request hits the browser cache.)






          share|improve this answer















          tl;dr




          RewriteCond %DOCUMENT_ROOT/%1.webp -f



          It should be $1, not %1 in the RewriteCond TestString. Although it's unclear why this behaviour should have only recently changed (or maybe it hasn't?).







          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT/%1.webp -f
          RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]



          This should never have worked, unless you had a file called ".webp" (with no basename, starting with a dot) in the document root. If you did, then it would have always rewritten the jpeg/png URL to <whatever>.webp, whether it existed or not.



          So, this would seem to suggest you do have a file called .webp (no basename) in the document root? (And/or you are seeing a cached redirect for some requests? See below...)



          The %1 backreference in the 2nd RewriteCond TestString should be $1, not %1. %1 is a backreference to the first captured group in the last matched CondPattern - of which there is none, so %1 is always empty. So, it will always test the existence of /path/to/document-root/.webp. $1 on the other hand is a backreference to the first captured group in the RewriteRule pattern, ie. the filesystem path/basename, excluding the file extension.



          You have used $1 in the RewriteRule substitution - you should have used the same in the preceding condition.



          Note, that if these directives are in a Virtual Host context then you should either... remove the slash in the RewriteCond TestString, for example:



          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT$1.webp -f
          RewriteRule (.+).(?:jpe?g|png)$ $1.webp [NC,T=image/webp,E=webp,L]


          OR, exclude the slash in the RewriteRule captured group. For example:



          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %DOCUMENT_ROOT/$1.webp -f
          RewriteRule ^/(.+).(?:jpe?g|png)$ /$1.webp [NC,T=image/webp,E=webp,L]


          Otherwise, you'll end up with a double slash in the TestString that the OS will need to resolve.




          RewriteCond %HTTP_ACCEPT image/webp
          RewriteCond %REQUEST_URI (.+).(?:jpe?g|png)$
          RewriteCond %DOCUMENT_ROOT/%1.webp -f
          RewriteRule .* %1.webp [NC,T=image/webp,E=webp,L]



          This achieves the same as simply using $1 in the original rule block (as mentioned above). Curious that you changed the backreference in the substitution to %1, but knew to leave the backreference in the condition unchanged (or was this simply overlooked)?




          During testing, I changed the original rewrite to a 301 redirect ...




          Always test with 302 (temporary) redirects. 301s are cached persistently by the browser, so can cause caching issues later. (In fact, this can account for "intermittent" behaviour when the request hits the browser cache.)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 28 at 23:46

























          answered May 28 at 21:48









          MrWhiteMrWhite

          6,59121426




          6,59121426







          • 1





            Crap!! I do have a .webp file in the directory root, probably resulting from an automated script that creates the .webp files accidentally creating this as well. About the %1 backreference yesterday, I had the idea that %1 is for rewrite condition and $1 is for rules. I had the incorrect %1 originally because I thought that since it was being used as part of a rewrite condition, it should be %1. I have it working now, thanks!

            – Mike Willis
            May 29 at 16:08












          • 1





            Crap!! I do have a .webp file in the directory root, probably resulting from an automated script that creates the .webp files accidentally creating this as well. About the %1 backreference yesterday, I had the idea that %1 is for rewrite condition and $1 is for rules. I had the incorrect %1 originally because I thought that since it was being used as part of a rewrite condition, it should be %1. I have it working now, thanks!

            – Mike Willis
            May 29 at 16:08







          1




          1





          Crap!! I do have a .webp file in the directory root, probably resulting from an automated script that creates the .webp files accidentally creating this as well. About the %1 backreference yesterday, I had the idea that %1 is for rewrite condition and $1 is for rules. I had the incorrect %1 originally because I thought that since it was being used as part of a rewrite condition, it should be %1. I have it working now, thanks!

          – Mike Willis
          May 29 at 16:08





          Crap!! I do have a .webp file in the directory root, probably resulting from an automated script that creates the .webp files accidentally creating this as well. About the %1 backreference yesterday, I had the idea that %1 is for rewrite condition and $1 is for rules. I had the incorrect %1 originally because I thought that since it was being used as part of a rewrite condition, it should be %1. I have it working now, thanks!

          – Mike Willis
          May 29 at 16:08

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Server Fault!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f969217%2fwhy-was-this-f-flag-returning-true%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

          What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

          Esgonzo ibérico Índice Descrición Distribución Hábitat Ameazas Notas Véxase tamén "Acerca dos nomes dos anfibios e réptiles galegos""Chalcides bedriagai"Chalcides bedriagai en Carrascal, L. M. Salvador, A. (Eds). Enciclopedia virtual de los vertebrados españoles. Museo Nacional de Ciencias Naturales, Madrid. España.Fotos