Content item and widget alternates based on the alias and slug, URL Rewriting Module - This week in Orchard (25/10/2024)

gabor.domonkos

Adding content item and widget alternates based on the alias and slug, retiring AssignRoles and AssignRole_{RoleName} permissions, and a demo about the URL Rewriting Module are the topics for this week. Let's check them out in detail!

Orchard Core updates

Retire AssignRoles and AssignRole_{RoleName} permission

In the Roles feature, there were previously AssignRoles and AssignRole_{RoleName} permissions, alongside the Users feature's AssignRoleToUsers and AssignRoleToUsers_{RoleName} permissions. As these permissions were redundant and the AssignRoles permissions were never actually authorized against, AssignRoles and AssignRole_{RoleName} have been removed in favor of the latter.

Please review all your recipes and replace occurrences of AssignRoles with AssignRoleToUsers, and AssignRole_{RoleName} with AssignRoleToUsers_{RoleName}.

Retire AssignRoles and AssignRole permission

Adding content item and widget alternates based on the alias and slug

From now on, content item shapes (Content and Widget local zones) can be overridden by their alias if AliasPart is attached or by their slug if AutoroutePart is attached. For examples, refer to the docs. For documentation about using Widget alternates for alias and slug, refer to the following page.

But before diving into the documentation, let's see a quick example of utilizing the Slug template! The Content__Slug__[Slug] template is called when displaying a content item with a specific slug (i.e., a path is assigned to the item). It needs to have the AutoroutePart attached to it. If you set up your site using the Blog recipe, you have the Blog Post content item with an AutoroutePart attached. We also have one predefined Blog Post content item where the permalink is blog/post-1. Based on the documentation we can create a cshtml or liquid file called Content-Slug-blog-post-1 to override this content item by its slug. Here, you can see, we created a liquid file with just some static content and put it into the Views folder of the TheBlogTheme theme.

Blog Post with Slug example

Demos

URL Rewriting Module

The URL Rewriting feature allows you to configure URL rewrites and redirects for incoming HTTP requests, significantly improving your site's SEO and user experience. This feature lets you control how URLs are presented to users and search engines. Let's see a small demo about how you can use it!

First, you must enable the URL Rewriting feature under Configuration -> Features. After that, you will find a new option in the menu under the Configuration one: URL Rewriting. Here you can find the Add Rule button. If you click on that button, you will find a pop-up where you can select which rule type you want to add. We have two available rule types:

  • Redirect Rule: The Redirect Rule is utilized to send users from one URL to another, which is particularly beneficial for maintaining SEO integrity when URLs change. For example: permanently redirect users from /about-us to /about.
  • Rewrite Rule: The Rewrite Rule allows you to modify the incoming request URL without changing the URL displayed in the browser's address bar, aiding in content organization. For example: change requests for media files from /img/ to /media/.

Let's add a Rewrite Rule for now! The Display Name of our rule will be Rule 1- img to media and the Match URL Pattern will be /img/(.*). It means that it will match every request that starts with /img and the next part is the name of the image. And the Substitution URL Pattern will be /media/$1. The (.*) means that match anything and this anything will be pasted back after the /media/$ part, by rewriting the $1 with the content of the (.*). All of this convention was inspired by the Apache mod_rewrite module which uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs on the fly. Now hit Save and try out our rule!

Creating a new Rewrite Rule

When using the Blog recipe, we have some images in the Media Library. The site displays the home-bg.jpg image when you are on the home page of your site. The URL of this image is /media/home-bg.jpg. Now let's try to change this URL to img/home-bg.jpg. And by doing that you should get the same result, the same image. What it means is we used img instead of media and when this URL was received by the server, it matched against the rule and internally it was changed outer to replace the img string with the media string. That's why Orchard Core could proceed with it and reply with the result.

Using img instead of media URL

And as you saw, there are several other options you can utilize when creating a URL Rewrite rule. If you want to know more about this new feature, head to the documentation where you can read about how you can create additional rule sources, how to use recipes for creating and updating rules, and much more! And as always, head to YouTube for a recording!

Orchard Dojo Newsletter

Lombiq's Orchard Dojo Newsletter has 468 subscribers! We have started this newsletter to inform the community around Orchard of the latest news about the platform. By subscribing to this newsletter, you will get an e-mail whenever a new post is published to Orchard Dojo, including This week in Orchard of course.

Do you know of other Orchard enthusiasts who would like to read our weekly articles? Tell them to subscribe here!

If you are interested in more news about Orchard and the details of the topics above, don't forget to check out the recording of this Orchard meeting!