Seo 使用HTTP\U主机在umbraco中生成完整的规范URL

Seo 使用HTTP\U主机在umbraco中生成完整的规范URL,seo,content-management-system,url-rewriting,Seo,Content Management System,Url Rewriting,我正在尝试为我的网页使用规范url。我正在做的是: 我需要页面的完整url,我通过以下代码生成: @{ var canonicalUrl= String.Empty; if(umbraco.library.RequestServerVariables("HTTP_HOST").ToLower().StartsWith("www")) { canonicalUrl = string.Concat("http://", umbraco.library.RequestServerVariable

我正在尝试为我的网页使用规范url。我正在做的是: 我需要页面的完整url,我通过以下代码生成:

@{
var canonicalUrl= String.Empty;

if(umbraco.library.RequestServerVariables("HTTP_HOST").ToLower().StartsWith("www")) {
  canonicalUrl = string.Concat("http://", umbraco.library.RequestServerVariables("HTTP_HOST"), CurrentPage.GetPropertyValue("umbracoUrlAlias"));
} else {
  canonicalUrl = string.Concat("http://www.", umbraco.library.RequestServerVariables("HTTP_HOST"), CurrentPage.GetPropertyValue("umbracoUrlAlias"));
}
<link rel="canonical" href="@canonicalUrl" />
}
@{
var canonicalUrl=String.Empty;
if(umbraco.library.RequestServerVariables(“HTTP_HOST”).ToLower().StartsWith(“www”)){
canonicalUrl=string.Concat(“http://”,umbraco.library.RequestServerVariables(“http_HOST”),CurrentPage.GetPropertyValue(“umbracoUrlAlias”);
}否则{
canonicalUrl=string.Concat(“http://www.,umbraco.library.RequestServerVariables(“HTTP_主机”)、CurrentPage.GetPropertyValue(“umbracoUrlAlias”);
}
}

我不确定,这是否是预期的方式。或者,如果有更好的办法。

这对我们以前很有用。。。看起来你已经在用了

在Umbraco上使用Razor语法(与XSLT相反)

@使用umbraco
@使用系统
@{var canonicalUrl=String.Empty;}
@if(umbraco.library.RequestServerVariables(“HTTP_HOST”).ToLower().StartsWith(“www”)){
canonicalUrl=string.Concat(“http://”,umbraco.library.RequestServerVariables(“http_HOST”),Model.Url);
}否则{
canonicalUrl=string.Concat(“http://www.,umbraco.library.RequestServerVariables(“HTTP_HOST”),Model.Url);}

以下是如何在Umbraco 7.1.x上使用Razor设置规范URL的指南

如果您确实希望URL中包含“www.”,请使用以下命令:

@using umbraco
@using System
@{var canonicalUrl= String.Empty;}
@if(umbraco.library.RequestServerVariables ("HTTP_HOST").ToLower().StartsWith("www")) {
canonicalUrl = string.Concat("http://",  umbraco.library.RequestServerVariables("HTTP_HOST"), Model.Url);
} else {
canonicalUrl = string.Concat("http://www.",  umbraco.library.RequestServerVariables("HTTP_HOST"), Model.Url);
}
<link rel="canonical" href="@canonicalUrl"  />
@使用umbraco
@使用系统
@{var canonicalUrl=String.Empty;}
@if(umbraco.library.RequestServerVariables(“HTTP_HOST”).ToLower().StartsWith(“www”)){
canonicalUrl=string.Concat(“http://”,umbraco.library.RequestServerVariables(“http_HOST”),Model.Url);
}否则{
canonicalUrl=string.Concat(“http://www.,umbraco.library.RequestServerVariables(“HTTP_主机”),Model.Url);
}
如果您不希望URL前面有“www.”,请改用此选项:

@using umbraco
@using System
@* empty out the string *@
@{var canonicalUrl= String.Empty;} 
@* check if  the requested URL starts with www. *@
@if(umbraco.library.RequestServerVariables("HTTP_HOST").ToLower().StartsWith("www"))  { 
@* adds http:// to the beginning *@
canonicalUrl = string.Concat("http://", umbraco.library.RequestServerVariables ("HTTP_HOST"), Model.Url); 
@* strips out the www. from the URL *@
canonicalUrl = umbraco.library.Replace(canonicalUrl,  "www.", ""); 
} else {
@* if they did not use the www prefix, we still have to add http:// to the URL *@
canonicalUrl = string.Concat("http://", umbraco.library.RequestServerVariables("HTTP_HOST"), Model.Url); 
}
<!--  output the canonical URL -->
<link rel="canonical" href="@canonicalUrl"  />
@使用umbraco
@使用系统
@*清空字符串*@
@{var canonicalUrl=String.Empty;}
@*检查请求的URL是否以www.com开头*@
@if(umbraco.library.RequestServerVariables(“HTTP_HOST”).ToLower().StartsWith(“www”){
@*将http://添加到开头*@
canonicalUrl=string.Concat(“http://”,umbraco.library.RequestServerVariables(“http_HOST”),Model.Url);
@*从URL中删除www*@
canonicalUrl=umbraco.library.Replace(canonicalUrl,“www.,”);
}否则{
@*如果他们没有使用www前缀,我们仍然必须将http://添加到URL*@
canonicalUrl=string.Concat(“http://”,umbraco.library.RequestServerVariables(“http_HOST”),Model.Url);
}


您首选的域名是否以“www.”开头?即其
http://www.example.com
作为
http://example.com
@using umbraco
@using System
@* empty out the string *@
@{var canonicalUrl= String.Empty;} 
@* check if  the requested URL starts with www. *@
@if(umbraco.library.RequestServerVariables("HTTP_HOST").ToLower().StartsWith("www"))  { 
@* adds http:// to the beginning *@
canonicalUrl = string.Concat("http://", umbraco.library.RequestServerVariables ("HTTP_HOST"), Model.Url); 
@* strips out the www. from the URL *@
canonicalUrl = umbraco.library.Replace(canonicalUrl,  "www.", ""); 
} else {
@* if they did not use the www prefix, we still have to add http:// to the URL *@
canonicalUrl = string.Concat("http://", umbraco.library.RequestServerVariables("HTTP_HOST"), Model.Url); 
}
<!--  output the canonical URL -->
<link rel="canonical" href="@canonicalUrl"  />