2011-03-21 54 views
0

我试图实现301重定向,使我的所有URL都重定向到给定页面的www版本。我们的页面结构是:url.com/home/default.cfm。我正尝试将ColdFusion中的URL重写为当前目录,而不使用文件名。在ColdFusion中没有文件名的URL重定向?

我使用的代码是这样的:

<cfif (CGI.SERVER_NAME NEQ "www.url.com")> 
<!-- Save the URL (and $_GET variables too) as the string 'strUrl' --> 
<!-- <cfset strUrl = CGI.script_name & "?" & CGI.query_string />--> 
<cfset strUrl = CGI.script_name /> 
<!-- Use 301 for SEO-friendly redirects --> 
<cfheader statuscode="301" statustext="Moved permanently"> 
<!-- Redirect to new website (this case, added www.) with strUrl added on --> 
<cfheader name="Location" value="http://www.url.com#strUrl#"> 
</cfif> 

这是如此的接近,除了CGI.script_name返回与文件名的路径。任何想法如何获得目录?请记住,我们可能有嵌套的目录,例如/ foo1/foo2 /。

谢谢。

回答

0

试试这个

<cfset strUrl = listDeleteAt(cgi.SCRIPT_NAME,listLen(cgi.SCRIPT_NAME,'/'),'/') />

0

或者试试这个

<cfset strUrl = Replace(cgi.SCRIPT_NAME, ListLast(cgi.SCRIPT_NAME,'/'), '')> 

有结尾的斜线为好。

1

你可以使用getDirectoryFromPath()来做到这一点,它也会保留最后的斜杠。

<cfset strUrl = getDirectoryFromPath(cgi.SCRIPT_NAME)> 

此外,通过使用cflocation标记并指定statusCode属性来简化代码。

<cfif (CGI.SERVER_NAME NEQ "www.url.com")> 
<!-- Save the URL (and $_GET variables too) as the string 'strUrl' --> 
<!-- <cfset strUrl = CGI.script_name & "?" & CGI.query_string />--> 
<cfset strUrl = getDirectoryFromPath(CGI.script_name) /> 
<!-- Use 301 for SEO-friendly redirects --> 
<cflocation url="http://www.url.com#strUrl#" statusCode="301"> 
</cfif> 
2

如果你的目的是要重定向所有流量将“domain.com”到“www.domain.com”,你可能会更好的网络服务器级别这样做。网络服务器不仅会处理CF文件,还会处理静态资源,如图像,cs & js文件等。

+0

+1非CF资源会发生什么情况?如果可用,让Web服务器处理这些东西。 – jfrobishow 2011-03-23 14:36:19

+0

是的,这将是我的偏好,但不幸的是我们的技术设置不允许这样 - 我们在一个大型公司,这使我们的服务器级别访问变得复杂。另外,我们这样做的主要原因是为了防止在Google中重复索引页面(这是以前的一个问题,我们有非WWW和WWW版本索引)。 – Rich 2011-03-24 04:36:11

+0

如果您正在专门为Google索引做这件事,您可以查看Google针对网站管理员的“首选域名”说明。请参阅[link] http://www.google.com/support/webmasters/bin/answer.py?answer=44231 – 2011-07-06 20:24:19