Bounties /Pin to ProfileBookmark

Force CloudFlare to add trailing slash with Page Rules

I’m working on a site that currently has both trailing slash and non-slashed URLs active. For example, visiting /page-name and /page-name/ both work and causing SEO canonical issues.

Using only CloudFlare, I’d like to write a Page Rule to solve this. I tried:

// URL to forward
*example.com/*

// Final 301 redirect destination
https://example.com/$2/

CloudFlare then gives this error and will not create the Page Rule:
“Forwarding URL matches the target and would cause a redirect loop.”

How can I resolve this?

to post a answer
January 20th, 2023Network Management

3 Answer(s)

pending review
@0xJan 23, 2023 — try:

var links = document.getElementsByTagName('a');
for (vari = 0; i < links.length; i++ {
var link = links[i];
if (link.href.indexOf('http') == 0) {
if (link.href.indexOf('/', link.href.indexOf('//') +
2) == -1) {
link.href += '/';
}
}
}
pending review
@attilalbJan 24, 2023 — You could try creating two separate page rules:

One rule should redirect all requests that have a trailing slash to the non-slashed version of the URL (e.g. redirect /page-name/ to /page-name.

The second rule should redirect all requests that do not have a trailing slash to the version of the URL with a trailing slash (e.g. redirect /page-name to /page-name/).

This way, you redirect all the traffic to the same url with or without trailing slash.

Rule 1:

// URL to forward
*example.com/*/
// Final 301 redirect destination
https://example.com/$1


Rule 2:

// URL to forward
*example.com/*
// Final 301 redirect destination:
https://example.com/$1/
@buildinteractiveThanks but the 2nd rule creates the same problem mentioned: "Forwarding URL matches the target and would cause a redirect loop."
pending review
@jordan-thirkleJan 26, 2023 — Why you're seeing the error
The error message you're seeing is because the URL you're trying to forward to is the same as the URL you're trying to match.

This would create a redirect loop.

How do you solve this?
A possible way to solve this is to:
Use a different pattern to match the non-trailing slash URLs and redirect them to the trailing slash version.

To do this you would:
Use a regular expression pattern to match URLs that do not have a trailing slash and redirect them to the same URL with a trailing slash.

An example of a Page Rule that could work:

URL to match: *example.com/page-name[^/]

Action: "Forwarding URL"

Status code: "301 - Permanent Redirect"

Destination URL: https://example.com/page-name/$1/


Steps to input this into CloudFlare:
Log in to your Cloudflare account and select the website for which you want to create the Page Rule.
Go to the "Page Rules" section and click on the "Create Page Rule" button.
In the "If the URL matches" field, enter the pattern you want to match, which is *example.com/page-name[^/] in the example I provided earlier.
Under "Then the settings are," select "Forwarding URL" from the dropdown menu.
In the "Status code" field, select "301 - Permanent Redirect"
In the "Destination URL" field, enter the URL you want to redirect to, which is https://example.com/page-name/$1/ in the example I provided earlier.
Click on the "Save and Deploy" button to create the Page Rule.

Note: it's good practice to use rel=canonical to specify the preferred version of the URL, search engines will use this to understand which version should be indexed.

You can repeat this process for any other pages that are having this issue and you can use a wildcard pattern to match all pages and redirect them to their respective trailing slash URLs.

Hope this helps!
@buildinteractiveThis seems specific to individual pages where you have "page-name" mentioned. How can I make this site-wide with a single Page Rule? And thanks for your time!
×

Success!

Help @buildinteractive spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with Github
about: ({
version: 0.1.7 BETA 2.4,
whats_new: article editor v2,
up_next: tip code demos,
coming_soon: embeddable tipping,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh

version: 0.1.5,
notes: added pins + new badge

version: 0.1.4,
notes: home refresh + tools
)...
recent_tips: (
tipper: @JohnDavis,
amount: 250 SATS,
source: article

tipper: @Megan0910,
amount: 1000 SATS,
source: article

tipper: @JohnDavis,
amount: 250 SATS,
source: article
)...