Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Support 1 post 81 karma points
    Sep 15, 2023 @ 09:28
    Support
    0

    URL Rewirte Kills image cropper?

    The version is 11.4.2 I set up a URL rewire for the site to remove the trailing slash. It works, however, the image generator stopped working on the front end. Urls like image.png?width=300 shows the original image.

    Not sure what took place here.

    Code in the startup.cs

    app.UseHttpsRedirection();
    
    var rewriteOptions = new RewriteOptions().AddIISUrlRewrite(env.ContentRootFileProvider, "IISUrlRewrite.xml");
    
    app.UseRewriter(rewriteOptions);
    
    // This line is needed for the rewrites to take effect.
    app.UseStaticFiles();
    
    
    
    app.UseUmbraco()
        .WithMiddleware(u =>
        {
            u.UseBackOffice();
            u.UseWebsite();
        })
        .WithEndpoints(u =>
        {
            u.UseInstallerEndpoints();
            u.UseBackOfficeEndpoints();
            u.UseWebsiteEndpoints();
        });
    

    Content in IISUrlRewrite.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <rewrite>
      <rules>
        <rule name="RemoveTrailingSlashRule1" enabled="true" stopProcessing="true">
          <match url="(.*)/$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/App_Plugins/" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/umbraco" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/media" negate="true" />
    
          </conditions>
          <action type="Redirect" url="{R:1}" />
        </rule>
      </rules>
    </rewrite>
    

    Thanks

  • Brendan Rice 538 posts 1100 karma points
    Dec 08, 2023 @ 01:25
    Brendan Rice
    100

    I think I got a fix for this. The code below checks if the request is for an item in the media folder and if not it applies the appBuilder.UseStaticFiles().

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            var rewriteOptions = new RewriteOptions()
                .AddIISUrlRewrite(env.ContentRootFileProvider, "IISUrlRewrite.xml");
    
            app.UseRewriter(rewriteOptions);
    
            // This line is needed for the rewrites to take effect.
            app.UseWhen(context => !IsDynamicImageRequest(context), appBuilder =>
            {
                appBuilder.UseStaticFiles();
            });
    
            app.UseUmbraco()
                .WithMiddleware(u =>
                {
                    u.UseBackOffice();
                    u.UseWebsite();
                })
                .WithEndpoints(u =>
                {
                    u.UseInstallerEndpoints();
                    u.UseBackOfficeEndpoints();
                    u.UseWebsiteEndpoints();
                    u.EndpointRouteBuilder.MapControllerRoute("sitemap", "sitemap.xml", new { controller = "Sitemap", action = "Sitemap" });
                });
        }
    
        private bool IsDynamicImageRequest(HttpContext context)
        {
            // Implement logic to determine if the request is for a dynamic image
            // For example, checking if the path starts with "/media/" and has query parameters like "?width="
            return context.Request.Path.StartsWithSegments("/media") && context.Request.QueryString.HasValue;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft