【问题标题】:Google Ajax Crawlable Rewrite!!? _escaped_fragment_=谷歌 Ajax 可抓取重写!!? _escaped_fragment_=
【发布时间】:2015-02-14 01:50:15
【问题描述】:

我已经使用 hash bang 开始了 ajax 站点路径

目前我的网址如下所示:

http://www.domain.com/#!/index
http://www.domain.com/#!/studio
http://www.domain.com/#!/about

来自阅读谷歌的文档:http://code.google.com/web/ajaxcrawling/

看来 google 会尝试将http://www.domain.com/#!/studio 重写为

http://www.domain.com/?_escaped_fragment_=studio

我想知道如何获得一个 IIS7 重写规则来将转义的片段重定向到:

http://www.domain.com/studio 即获取查询字符串 arg 并将其映射回根

该站点是在 asp.net 中使用 umbraco 完成的,因此我也可以从 umbraco 访问重写配置文件!

干杯

【问题讨论】:

    标签: asp.net ajax hash


    【解决方案1】:

    @Giberno : 看来你不明白 pennylane 在问什么。

    pennylane 尝试通过 IIS/web.config 重定向的全部原因是搜索引擎机器人被重定向到静态预生成的 .htm/.html 文件。

    下一代机器人,如 googlebot,可以识别 hashbang。

     hashbang = '#!' in 'http://example.com/#!/some/page/with/ajax/content'
    

    当机器人在 url 中检测到这个 hashbang 时,它会将 url 转换为 _escaped_fragment_ 查询字符串 url。

    上面的例子将被转换为:

    'http://example.com/?_escaped_fragment_=/some/page/with/ajax/content'
    

    这是因为机器人无法执行 javascrip。然而你给出了一个javascript解决方案?如果我错了,请启发我。

    更多信息AJAX Applications and SEO

    @pennylane:我也在尝试做同样的事情,我想我明白了。

    情况:

    当我浏览到快照时,快照会显示状态码 200 OK。

    我为 web.config 文件设置了以下重写规则:

    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
    
                    ... other rewrite rules ...
    
                    <rule name="EscapedFragment" stopProcessing="true">
                        <match url="(.*)"/>
                        <conditions>
                            <add input="{QUERY_STRING}" pattern="_escaped_fragment_=/([_0-9a-zA-Z-]+)" />
                        </conditions>
                        <action type="Rewrite" url="snapshots/snapshot_{C:1}.html" appendQueryString="false" />
                    </rule>  
    
                    ... other rewrite rules ...
    
                </rules>
            </rewrite>
    
            ... other configs ...
    
        </system.webServer>
    </configuration>
    

    重定向规则说明:

    匹配:

    <match url="(.*)"/>
    

    在任何 url 上调用该规则。

    条件:

    <add input="{QUERY_STRING}" pattern="_escaped_fragment_=/([_0-9a-zA-Z-]+)" />
    

    如果 '_escaped_fragment_=/' 后跟一个字母数字字符串,有或没有大小写字符、下划线 (_) 或连字符 (-) ...

    重写动作:

    <action type="Rewrite" url="snapshots/snapshot_{C:1}.html" appendQueryString="false" />
    

    在不附加查询字符串的情况下重写 url。

    其中 {C:1} = '_escaped_fragment_' 查询字符串参数的值

    信息来源: 我根据以下信息构建了这个重写规则:

    结果:

    浏览到:

    'http://example.com/?_escaped_fragment_=/test'
    

    应该改写为:

    GET 'http://example.com/snapshots/snapshot_test.html'
    

    测试模式 {C:1} = 'test'。

    测试:

    在我看来,测试重写规则是否有效的最简单方法是按照以下步骤操作:

    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>URL Rewrite Module Test</title>
    </head>
    <body>
          <h1>URL Rewrite Module Test Page</h1>
          <table>
                <tr>
                      <th>Server Variable</th>
                      <th>Value</th>
                </tr>
                <tr>
                      <td>Original URL: </td>
                      <td><%= Request.ServerVariables["HTTP_X_ORIGINAL_URL"] %></td>
                </tr>
                <tr>
                      <td>Final URL: </td>
                      <td><%= Request.ServerVariables["SCRIPT_NAME"] + "?" + Request.ServerVariables["QUERY_STRING"] %></td>
                </tr>
          </table>
    </body>
    </html>
    
    • 在包含上述代码的同一目录下创建一个 snapshot_test.aspx 文件。
    • 将重写规则更改为重写为 .aspx 文件而不是 .html 文件
    • 最后在浏览器中输入以下网址:

      'http://example.com/?_escaped_fragment_=/test'

    For more info on IIS rewrite testing

    另外:

    为规范 url 和 sitemap.xml 使用漂亮的 url。

    Pretty url: 'http://example.com/#!/test'
    Ugly url: 'http://example.com/?_escaped_fragment_=/test'
    

    【讨论】:

      【解决方案2】:

      尝试做:

      javascript代码:

      var hashchange;
      function MyHash() {
          if(window.location.hash) {
             MyHash = window.location.hash.replace("#!/", "");
              $.get("process_page.asp?_escaped_fragment_=" + MyHash, function(data) {
                  $("#content").html(data);
              });
          }
      }
      $(document).ready(function() {
          hashchange();
      });
      
      $(window).bind('hashchange', function() {
          hashchange();
      });
      

      HTML 代码:

      <div id="content"><!--#include file="process_page.asp"--></div>
      

      在process_page.asp中:

      对GET_escaped_fragment_做点什么

      也可以参考:http://www.gingerhost.com/ajax-demo/

      顺便说一句:我不擅长asp。我的主要语言是php。但我做了上面的代码,不需要url重写

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        • 2013-04-12
        • 2016-03-06
        • 2017-06-08
        • 2016-04-22
        • 2018-06-20
        • 1970-01-01
        相关资源
        最近更新 更多