Skip to content

Instantly share code, notes, and snippets.

@blowsie
Last active September 6, 2021 22:26
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save blowsie/04c183c62a432f6450c9 to your computer and use it in GitHub Desktop.
Save blowsie/04c183c62a432f6450c9 to your computer and use it in GitHub Desktop.
Prerender IIS Web.config
<?xml version="1.0"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Prerender-Token" value="XXXXXXXXXXXXX" />
</customHeaders>
</httpProtocol>
<rewrite>
<rules>
<!--# Only proxy the request to Prerender if it's a request for HTML-->
<rule name="Prerender" stopProcessing="true">
<match url="^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent))(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_USER_AGENT}" pattern="baiduspider|facebookexternalhit|twitterbot" />
<add input="{QUERY_STRING}" pattern="_escaped_fragment_" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="http://service.prerender.io/http://yourwebsite.com/{R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
@ktriple
Copy link

ktriple commented Jul 1, 2019

If anyone ever encounters this issue, I've tried the stuff above and got it working after many hours by mixing some things up.

1. In your Web App root directory (/site/wwwroot), add or update your web.config file with this:

<configuration>
  <system.webServer>     
    <rewrite>     
        <rules>
            <rule name="Prerender" stopProcessing="true">
                <serverVariables>
                    <set name="HTTP_X_PRERENDER_TOKEN" value="**YOUR TOKEN**" />
                </serverVariables>
                <match url="^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent))(.*)" ignoreCase="false" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTP_USER_AGENT}" pattern="bingbot|googlebot|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator" />
                </conditions>
                <action type="Rewrite" url="https://service.prerender.io/https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite>

  </system.webServer>
</configuration>

2. In /site/ create a file named applicationHost.xdt and add this content:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
        <proxy xdt:Transform="InsertIfMissing" enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false" />
        <rewrite>
            <allowedServerVariables>
                <add name="HTTP_X_PRERENDER_TOKEN" xdt:Transform="InsertIfMissing" />
            </allowedServerVariables>
        </rewrite>
    </system.webServer>
</configuration>

**3. Restart your Web App service (click 'Stop' and 'Start', not only 'Restart').

4. Test with a tool like: https://technicalseo.com/tools/fetch-render/

Hope this helps someone.

@masanchezro
Copy link

masanchezro commented Sep 6, 2021

This worked for us. Our rewrite is as follows:

        <rule name="Seo rewrite rule" stopProcessing="true">
          <serverVariables>
            <set name="HTTP_X_PRERENDER_TOKEN" value="[...your token...]" />
          </serverVariables>
          <conditions>
            <add input="{QUERY_STRING}" pattern="(.*)_escaped_fragment_=(.*)" />
          </conditions>
          <action type="Rewrite" url="http://service.prerender.io/http://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
        </rule>

As far as the "allowableServerVariables", we handled this through the IIS and clicking either the Default Web Site of the Site itself, then clicking the URL Rewrite module icon, then select View Server Variables, and add the HTTP_X_PRERENDER_TOKEN variable name to the list.

With this in place, we were now able to append the escaped_fragment querystring to our urls and see the rewritten results after coming back through the server. You should also be able to verify in your Account page on PreRender.io that the pages are being cached.

HTH,
Randy

Hi!!

I spend a lot days for the solution, but i add the variable and works fine!!!

Solution for me :

Only exist the web.config with the configuration (Htm5 redirect and this) , i dont need it applicationHos.xdt.:


<rewrite>
          <rules>
              <!--# Only proxy the request to Prerender if it's a request for HTML-->
              <rule name="SEO" stopProcessing="true">
              <match url="^(?!.*?(\.js|\.css|\.xml|\.html|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.woff|\.ttf|\.m4v|\.svg|\.torrent))(.*)" ignoreCase="false" />
              <conditions logicalGrouping="MatchAny">
                  <add input="{HTTP_USER_AGENT}" pattern="facebookexternalhit|twitterbot|googlebot|whatsapp" />
                  <add input="{QUERY_STRING}" pattern="(.*)_escaped_fragment_=(.*)" ignoreCase="false" />
              </conditions>
                  <action type="Rewrite" url="http://service.prerender.io/http://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" /> 
              <serverVariables>
                <set name="HTTP_X_PRERENDER_TOKEN" value="yourtoken" /> 
              </serverVariables>
              </rule>
              <rule name="Redirect To Index" stopProcessing="true">
                  <match url=".*" />
                  <conditions>
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                  <action type="Rewrite" url="/" />
              </rule> 
          </rules>
      </rewrite>

The next step was as you indicates ; add into Rewrite module icon, then select View Server Variables, the new variable name "HTTP_X_PRERENDER_TOKEN" and works !! amazing =)!!

Thnks for your time.

I want to share my test options ;

1.-https://technicalseo.com/tools/fetch-render/
2.-https://developers.facebook.com/tools/debug

Any ask or something im here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment