Rewrite file.jpg to file.jpg.webp [Answered]RSS

9 replies

Last post Oct 03, 2019 01:41 AM by Jalpa Panchal

  • Rewrite file.jpg to file.jpg.webp

    Oct 01, 2019 01:24 AM|Bobbymale|LINK

    I am not able to rewrite the file.jpg to file.jpg.webp. This rewrite module not doing anything and the jpg file still being displayed as jpg and the url did not change.

    <rewrite>
    <rules>
    <rule name="webp">
    <match url="(.+)\.(jpe?g|png)$" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll">
    <add input="{HTTP_ACCEPT}" pattern="image/webp" ignoreCase="false" />
    <add input="{DOCUMENT_ROOT}/{R:0}.webp" matchType="IsFile" />
    </conditions>
    <action type="Rewrite" url="{R:0}.webp" logRewrittenUrl="true" />
    <serverVariables>
    <set name="ACCEPTS_WEBP" value="true" />
    </serverVariables>
    </rule>
    </rules>
    <outboundRules>
    <rule name="Set Vary header" preCondition="IsWebp">
    <match serverVariable="RESPONSE_Vary" pattern=".*" />
    <action type="Rewrite" value="Accept"/>
    </rule>
    <preConditions>
    <preCondition name="IsWebp">
    <add input="{ACCEPTS_WEBP}" pattern="true" ignoreCase="false" />
    </preCondition>
    </preConditions>
    </outboundRules>
    </rewrite>

  • Re: Rewrite file.jpg to file.jpg.webp

    Oct 02, 2019 03:20 AM|Jalpa Panchal|LINK

    Hi,

    which browser you are trying to use? webp image only supports chrome and opera browser.

    your rule is correct and working well but there is one issue.

    you need to add ACCEPTS_WEBP server variable.

    and another thing add webp mime type.

    <staticContent>
            <mimeMap fileExtension=".webp" mimeType="image/webp" />
        </staticContent>
    

    your rule only changes the image from jpg to webp but does not change the file extension.

    to rewrite file extention you need to use another outbound rule:

    <outboundRules>
    
     <rule name="jpg to webp" preCondition="ResponseIsHtml1" enabled="true">
                        <match filterByTags="Img" pattern="(.+)\.(jpe?g|png)$" />
                        <action type="Rewrite" value="{R:1}.webp" />
                    </rule>
                    <preConditions>
                      
                
                        <preCondition name="ResponseIsHtml1">
                            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                        </preCondition>
                    </preConditions>
                
    </outboundRules>

    Regards,

    Jalpa

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: Rewrite file.jpg to file.jpg.webp

    Oct 02, 2019 03:26 AM|Jalpa Panchal|LINK

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: Rewrite file.jpg to file.jpg.webp

    Oct 02, 2019 09:56 AM|Bobbymale|LINK

    Got 500 - Internal server error. When I remove the lines below, they came up. No event log recorded either.

    <rule name="jpg to webp" preCondition="ResponseIsHtml" enabled="true">
    <match filterByTags="Img" pattern="(.+)\.(jpe?g|png)$" />
    <action type="Rewrite" value="{R:1}.webp" />
    </rule>

  • Re: Rewrite file.jpg to file.jpg.webp

    Oct 02, 2019 09:58 AM|Bobbymale|LINK

    HTTP Error 500.52 - URL Rewrite Module Error.

    Outbound rewrite rules cannot be applied when the content of the HTTP response is encoded ("gzip").

  • Re: Rewrite file.jpg to file.jpg.webp

    Oct 02, 2019 10:00 AM|Bobbymale|LINK

    This entire code does the trick. To by pass the gzip compression I added this  rewriteBeforeCache="true" so the outbound rules are processed first before compression. Thank you for all your help.

    <rewrite>
    <rules>
    <rule name="webp">
    <match url="(.+)\.(jpe?g|png)$" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll">
    <add input="{HTTP_ACCEPT}" pattern="image/webp" ignoreCase="false" />
    <add input="{DOCUMENT_ROOT}/{R:0}.webp" matchType="IsFile" />
    </conditions>
    <action type="Rewrite" url="{R:0}.webp" logRewrittenUrl="true" />
    <serverVariables>
    <set name="ACCEPTS_WEBP" value="true" />
    </serverVariables>
    </rule>
    </rules>
    <outboundRules rewriteBeforeCache="true">
    <rule name="jpg to webp" preCondition="ResponseIsHtml" enabled="true">
    <match filterByTags="Img" pattern="(.+)\.(jpe?g|png)$" />
    <action type="Rewrite" value="{R:0}.webp" />
    </rule>
    <preConditions>
    <preCondition name="ResponseIsHtml">
    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
    </preCondition>
    </preConditions>
    </outboundRules>
    </rewrite>

  • Re: Rewrite file.jpg to file.jpg.webp

    Oct 02, 2019 10:03 AM|Jalpa Panchal|LINK

    if you disable Dynamic compression it will also resolve HTTP Error 500.52 - URL Rewrite Module Error.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: Rewrite file.jpg to file.jpg.webp

    Oct 02, 2019 03:45 PM|Bobbymale|LINK

    Whats the fallback for other browsers? Can't think of good solutions....rewrite filter based on agent?

  • Re: Rewrite file.jpg to file.jpg.webp

    Oct 02, 2019 03:59 PM|Bobbymale|LINK

    Even just picking Chrome user agent IE and Edge ignores it and served with webp file format. 

    <rewrite>
    <rules>
    <rule name="webp">
    <match url="(.+)\.(jpe?g|png)$" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll">
    <add input="{HTTP_ACCEPT}" pattern="image/webp" ignoreCase="false" />
    <add input="{HTTP_USER_AGENT}" pattern="Chrome" />
    <add input="{DOCUMENT_ROOT}/{R:0}.webp" matchType="IsFile" />
    </conditions>
    <action type="Rewrite" url="{R:0}.webp" logRewrittenUrl="true" />
    <serverVariables>
    <set name="ACCEPTS_WEBP" value="true" />
    </serverVariables>
    </rule>
    </rules>
    <outboundRules rewriteBeforeCache="true">
    <rule name="jpg to webp" preCondition="ResponseIsHtml" patternSyntax="ExactMatch">
    <match filterByTags="Img" pattern="(.+)\.(jpe?g|png)$" />
    <action type="Rewrite" value="{R:0}.webp" />
    </rule>
    <preConditions>
    <preCondition name="ResponseIsHtml">
    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
    <add input="{HTTP_USER_AGENT}" pattern="Chrome" />
    </preCondition>
    </preConditions>
    </outboundRules>
    </rewrite>

  • Re: Rewrite file.jpg to file.jpg.webp

    Oct 03, 2019 01:41 AM|Jalpa Panchal|LINK

    the .webp file extension is not supported by some browser. there is no issue with the URL rewrite rule. 

    My teste result with different browser as below:

    1)Chrome: supported

    2)firefox: supported

    3)ie: not supported

    4)edge: supported

    5)opera: supported 

    You need to get more information regarding to the webp image format and supported browser and related setting. 

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.