Single Redirect from URL ending in /? to non

In Cloudflare I have a redirect rule

This is the expression

(ends_with(http.request.uri, "/?"))

I am trying to redirect any requests that end with /? To remove /? And reload that page.

eg. domain.com/page/? redirects to domain.com/page

Below this is a ‘then…’

I have the options ‘preserve query string’ which I checked, type of dynamic or static, I am choosing dynamic, but then the next field says expression.

What kind of expression would do what I am needing?

This one fails because of my plan

regex_replace("/foo/bar", "/bar$", "/baz") == "/foo/baz"

This one works, but it doesn’t do anything.

http.request.uri.path

This one doesn’t work, why?

http.request.uri.path.replace(/\/\?$/, '/')

I can’t find any documentation on valid expressions for these redirects.

Cloudflare, it’d be great to discuss valid expression formats on this page

Heres a photo.

Hi,

That expression is not what’s in your screenshot. Here you’re using URI, there you’re using URI Path. URI Path will never contain ?.

Also, it wouldn’t make sense to preserve query string in this case, as that would be an empty query string:

https://example.com/?
URI Full: https://example.com/?
URI: /?
URI Path /
Query String: [empty]

So if you want to redirect URLs ending with ?, which means an URL without a query string, but with the query string delimiter, you could use:

URI ends with ? == ends_with(http.request.uri, "?")

Then
URL Redirect
Type: Dynamic - Expression: concat("https://example.com", http.request.uri.path)
Preserve Query String (unchecked)

2 Likes

Thank you.

I tried that, no difference. See photos.

I made it say domain.com to hide the true domain.

In this photo, I stay on the page, no redirect.
image

image

Use URI instead of URI Path. The expression should read:

URI | ends with | ?

2 Likes

Still having issues.

As we see, no redirect happening
Screen Shot 2023-05-26 at 4.51.57 PM

I don’t think cloduflare can redirect this type of url. See photo. This isn’t working either.

Is your domain (the one you are redirecting FROM) :orange: on the DNS panel?

1 Like

Yes it is.

This works on my php site. But if anyone can tell me how to do this in cloudflare it’d be great.

function redirect_question_mark_urls() {
    $requested_url = $_SERVER['REQUEST_URI'];
    if (preg_match('/^(.*?)\/\?$/', $requested_url, $matches)) {
        $redirect_url = untrailingslashit($matches[1]);
        wp_redirect($redirect_url, 301);
        exit;
    }
}

I’ve just told you and have now created the same Redirect Rule on one of my domains, please test it here.

You created it, I did too.

But did yours work? Mine is not.

Did you click on the link? You can try other paths in that domain, just add a final ? and see what happens.

Sorry… I didn’t see it, I do now.

I see it working. Hmm. It will not do that on my site.

Well if you got it working with what you’ve told me, than cloudflare can do this. And something weird is going on with my site. Thank you.

1 Like

Make sure you don’t have any other redirect rule applying to similar condition.

It’s my only redirect rule. I have one other redirect that under page rules that has nothing to do with this sort of behavior.

That rule does contain this kind of target

.com/customer_login/?action=register

And redirects it to another page. Would this interfere with the other /? redirect rule?

1 Like

No, I don’t think so. Do you mind to share your domain name? I can message you privately if you prefer.

Privately would be preferred sure, thank you so much!

1 Like

I can see the redirect here:

image

Clear your browser cache, or perhaps Cloudflare cache for the URLs you are trying.

1 Like

I’ve chosen the clear everything on cloudflare cache.

I’ve tried a URL while in incognito mode, still can’t get it to work.