【问题标题】:Generating complete Canonical URL in umbraco using HTTP_HOST使用 HTTP_HOST 在 umbraco 中生成完整的规范 URL
【发布时间】:2014-02-20 09:59:06
【问题描述】:

我正在尝试为我的网页使用规范网址。我正在做的是: 我想要页面的完整 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" />
}

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

【问题讨论】:

  • 您的首选域是否以“www.”开头? IE。它的http://www.example.comhttp://example.com 的姿势

标签: seo content-management-system url-rewriting


【解决方案1】:

这是关于如何在 Umbraco 7.1.x 上使用 Razor 设置规范 URL 的指南。

如果您确实想要“www”。在 URL 中,使用这个:

@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"  />

如果您不想要“www”。在 URL 前面,然后改用这个:

@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"  />


Source - Canonical URLs in Umbraco CMS version 7.1.x with Razor

【讨论】:

    【解决方案2】:

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

    在 Umbraco 上使用 Razor 语法(相对于 XSLT)...

    @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" />
    

    Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      相关资源
      最近更新 更多