Coldfusion 如何将dynamic sitemap.xml添加到CFWheels应用程序?

Coldfusion 如何将dynamic sitemap.xml添加到CFWheels应用程序?,coldfusion,sitemap,coldfusion-9,cfwheels,Coldfusion,Sitemap,Coldfusion 9,Cfwheels,如何配置CFWheels以在上显示以下XML 我是否需要向routes.cfm添加一个条目?首先,如果您尚未配置Web服务器,则必须将其配置为执行完整的URL重写。这样,您就不必在URL中包含index.cfm(http://mydomain.com/index.cfm/foo/bar 变成) 设置好后,修改config/routes.cfm,如下所示: <cfset addRoute(name="sitemap", pattern="/sitemap.xml",

如何配置CFWheels以在上显示以下XML


我是否需要向routes.cfm添加一个条目?

首先,如果您尚未配置Web服务器,则必须将其配置为执行完整的URL重写。这样,您就不必在URL中包含index.cfm(http://mydomain.com/index.cfm/foo/bar 变成)

设置好后,修改config/routes.cfm,如下所示:

<cfset addRoute(name="sitemap",
        pattern="/sitemap.xml",
        controller="sitemap",
        action="list") />
请注意“sitemap.xml”。将其从列表中删除,留下以下内容:

RewriteCond %{REQUEST_URI} !^.*/(flex2gateway|jrunscripts|cfide|cfformgateway|cffileservlet|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|rewrite.cfm|favicon.ico)($|/.*$) [NC]
您可能需要重新启动/重新加载web服务器。但这应该可以做到

编辑

最后一个想法-您可以在Web服务器中添加一个重写规则,将对/sitemap.xml的请求重定向到/sitemap,因为您知道该规则是有效的。

设置控制器 控制器的
index()
方法应该如下所示。它存储在
控制器/Sitemap.cfc

function init() {
    // Grab data about URLs from model or build an array of structs to pass to the view
    urls = model("page").findAll(); // This line is just an example

    // Call `renderWith()` to instruct Wheels that this requires a special content-type
    renderWith(urls);
}
设置视图 然后,您在
views/sitemap/index.xml.cfm
上的视图可以生成所需的xml:

<cfoutput>

<?xml version="1.0" encoding="UTF-8"?>
<urlset
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

    #includePartial(partial="url.xml", query=urls)#
</urlset>

</cfoutput>
请记住,当您使用类似这样的分部时,查询列或结构键会被放置到
arguments
范围中,这就是我在虚构的示例中引用
arguments.uri
arguments.updatedAt
的原因

通过URL访问 根据服务器的URL重写功能,您可能需要尝试一些方法来让URL实现您想要的功能

您可以在
config/routes.cfm
中执行类似的操作(但我只在Apache上测试过):

同样,您可以在
http://www.example.com/sitemap.xml

最后,如果这不起作用,请从
config/routes.cfm
中删除多余的行,并加载此URL(不管怎样,它肯定会一直起作用):


这在寻址时不起作用。它确实对你有用。为了删除布局,我添加了
renderWith(data=“#url#”,layout=“false”)
到控制器中的
list()
函数。从重写模式中删除“sitemap.xml”是我做的第一件事。在我的例子中,我在使用IIS重写模块时从web.config文件中删除了它。还有其他想法吗?谢谢你帮助杰克。您修改重写的建议确实可以进行一些修改,您可以得到一些要点,感谢您提供了深入的示例。不幸的是,
http://www.example.com/sitemap.xml
<代码>http://www.example.com/sitemap/有效,但
http://www.example.com/sitemap.xml
最终触发onmissingtemplate.cfm并显示“未找到文件!”消息。您可能希望比较GitHub repo中的
web.config
.htaccess
,看看是否可以精细化
web.config
以更好地处理路径中的点。你能看看这个补丁是否有效吗?看起来有什么东西在1.1.7版的CFML中坏了。我看你接受了我的回答。我上面评论中的补丁有用吗?是的,谢谢!使用
sitemap.[format]
。我应该提到的是,对于那些不希望应用此修补程序的人,还有另一个选项。
RewriteCond %{REQUEST_URI} !^.*/(flex2gateway|jrunscripts|cfide|cfformgateway|cffileservlet|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|rewrite.cfm|favicon.ico)($|/.*$) [NC]
function init() {
    // Grab data about URLs from model or build an array of structs to pass to the view
    urls = model("page").findAll(); // This line is just an example

    // Call `renderWith()` to instruct Wheels that this requires a special content-type
    renderWith(urls);
}
<cfoutput>

<?xml version="1.0" encoding="UTF-8"?>
<urlset
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

    #includePartial(partial="url.xml", query=urls)#
</urlset>

</cfoutput>
<cfoutput>

<url>
    <loc>#arguments.uri#</loc>
    <loc>#arguments.updatedAt#</loc>
</url>

</cfoutput>
<cfset addRoute(pattern="sitemap.[format]", controller="sitemap", action="index")>
<cfset addRoute(pattern="sitemap", controller="sitemap", action="index")>
<cfset addRoute(pattern="sitemap.xml", controller="sitemap", action="index")>
<cfset addRoute(pattern="sitemap", controller="sitemap", action="index")>
`http://www.example.com/sitemap?format=xml`.