日本語版
最新ニュース
企業

CMS 12 の URL からセグメントを削除する

問題: さまざまなテンプレート (1 列、2 列など) を持つスケジュール ジョブを使用して数千のページを動的に作成し、特定のコンテナー ページ テンプレートの 1 つに保存しましたが、一部のページの URL の名前が変更されました。 つまり、私には2つの問題があります: URLからコンテナページ名セグメントを無視する方法 アドオンを使用せずに、または古いページを新しい URL にマッピングせずに、新しいページにリダイレクトします。 ページ階層 URL内の名前 新しい URL (シンプルなアドレス URL) 期待される結果 スタートページ -> コンテナページ -> 1 列ページ 勉強 学生/勉強 - URL から「container-page」セグメントを削除します。 - アドオンを追加せずに新しいURLに301リダイレクトを適用する…

CMS 12 の URL からセグメントを削除する

1718950622
2024-06-21 04:58:46

問題: さまざまなテンプレート (1 列、2 列など) を持つスケジュール ジョブを使用して数千のページを動的に作成し、特定のコンテナー ページ テンプレートの 1 つに保存しましたが、一部のページの URL の名前が変更されました。

つまり、私には2つの問題があります:

  1. URLからコンテナページ名セグメントを無視する方法
  2. アドオンを使用せずに、または古いページを新しい URL にマッピングせずに、新しいページにリダイレクトします。

ページ階層

URL内の名前

新しい URL (シンプルなアドレス URL)

期待される結果

スタートページ -> コンテナページ -> 1 列ページ

勉強

学生/勉強

– URL から「container-page」セグメントを削除します。

– アドオンを追加せずに新しいURLに301リダイレクトを適用する

https://local.alloy.com/container-page/study

https://local.alloy.com/students/study

https://local.alloy.com/study

https://local.alloy.com/students/study

解決上記の問題を解決するには、各ページに新しい URL のシンプルなアドレスを追加し、カスタム 301 リダイレクトを実装する必要があります。

ミドルウェアを登録する スタートアップ.cs URLからコンテナページセグメントをバイパスするファイル

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration)
 {
      app.UseMiddleware();
 }

シンプルなアドレスを読み取り、301 リダイレクトを適用するコンテナー ページ ミドルウェアを作成します。

public class SkipContainerPageMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ISettingsService _settingsService;
    private readonly IContentLoader _contentLoader;

    public SkipContainerPageMiddleware(RequestDelegate next, ISettingsService settingsService, IContentLoader contentLoader)
    {
        _next = next;
        _settingsService = settingsService;
        _contentLoader = contentLoader;
    }

    public async Task Invoke(HttpContext context)
    {
       //Create a content reference type property in GlobalPageSettings or HomePage to read the container page location where all pages created
        var folderLocation = _settingsService.GetSiteSettings()?.OEmbedFolderLocation ?? ContentReference.EmptyReference; 
        var folderName = "http://world.optimizely.com/" + _contentLoader.Get(folderLocation).Name;

        if (context.Request.Path.StartsWithSegments(folderName, StringComparison.OrdinalIgnoreCase))
        {
            context.Request.Path = context.Request.Path.HasValue
                ? context?.Request?.Path.Value.Substring(folderName.Length)
                : string.Empty;

            if (context != null)
            {
                var url = GetSimpleAddress(folderName, context);
                if (url != null)
                {
                    var newURl = $"{context.Request.Scheme}://{context.Request.Host}/{url.Trim("http://world.optimizely.com/")}";
                    context.Response.Redirect(newURl, true);
                    return;
                }
            }
        }

        if (context != null)
            await _next(context);
    }

    private string? GetSimpleAddress(string parentSegment, HttpContext context)
    {
        var parentPage = _contentLoader
            .GetChildren(ContentReference.StartPage)
            .FirstOrDefault(x => string.Equals(parentSegment.Trim("http://world.optimizely.com/"), x.URLSegment, StringComparison.InvariantCultureIgnoreCase))?
            .ContentLink
            ?? ContentReference.EmptyReference;

        var url = context.Request.Path.HasValue ? context?.Request.Path.Value.Trim("http://world.optimizely.com/") : string.Empty;
        if (string.IsNullOrWhiteSpace(url) || parentPage == ContentReference.EmptyReference)
            return default;

        PageData? page = null;
        foreach (var item in url.Split("http://world.optimizely.com/"))
        {
            page = RecursivePageBySegment(parentPage, item);
            if (page != null)
                parentPage = page.ContentLink;
        }

        return page?.ExternalURL ?? url;
    }

    private PageData? RecursivePageBySegment(ContentReference parentPage, string urlSegment)
    {
        var children = _contentLoader.GetChildren(parentPage);
        if (children.Count() == 0)
        {
            return _contentLoader.Get(parentPage);
        }

        var page = children.FirstOrDefault(page => page.URLSegment.Equals(urlSegment, StringComparison.OrdinalIgnoreCase));
        return page ?? default;
    }
}

このブログが、同様の機能を実現するのに役立つことを願っています。

2024年6月21日

#CMS #の #URL #からセグメントを削除する

執筆者について: nipponese

Nipponese News編集部は、国内外のニュースを日本語で分かりやすくお届けします。