Issue with a _redirects file

I want to redirect users from pages with double slashes in the URL to the same URL but with a single slash. For example:
from examplecom//some-page
to examplecom/some-page

.htaccess file, the following code didn’t work for me:

# Remove double forward slashes
<IfModule mod_alias.c>
	RedirectMatch 301 ^//(.*)$ http://example.com/$1
</IfModule>

<IfModule mod_rewrite.c>
    RewriteBase /

    # rule 1: remove multiple leading slashes (directly after the TLD)
    RewriteCond %{THE_REQUEST} \s/{2,}
    RewriteRule (.*) $1 [R=301,L]

    # rule 2: remove multiple slashes in the requested path
    RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
    RewriteRule (.*) %1/%2 [R=301,L]
</IfModule>

All redirect-related features in the dashboard didn’t work for me either. Nothing changed at all.

Only using a _redirect file worked, but with a loop issue, and I have no idea why it happens. Here is how the _redirects content is looks like:

*some redirects here*
//:path /:path 308

All pages with // (examplecom//some-page) in the URL successfully redirected to their copy with a single slash (examplecom/some-page), but then it goes into a redirect loop for some reason, so examplecom/some-page redirects to itself continuously. Can someone help me with that?

Have you tried URL Normalization?

https://dash.cloudflare.com/?to=/:account/:zone/rules/settings

Yes, I guess so

Have you tried normalization to the origin?

I just tried that - nothing changed. Thanks :slight_smile:

But it should. Is the domain :orange: Proxied by Cloudflare?

Yes it is

Is Normalization to Origin active now?
Is the Normalization Type set to “Cloudflare”?

Yes, correct, here is a screenshot.
Cache cleared too

And how are you assessing whether it is working? One thing you can try is to set a header to echo back the URI as received by the origin server:

Header set X-Request_URI %{REQUEST_URI}e

Then visit a URL with // and see if the double slash is reaching the origin by checking the value of the X-Request_URI header.

simply trying to open a link with two slashes. Isn’t that enough?

Can you help with that? I have no idea where I can configure that.

Just copy that line into your .htaccess file. After testing, you should remove it.

Is it okay that I don’t see that header in the respond?

My .htaccess content Is:

# Remove double forward slashes
<IfModule mod_alias.c>
	RedirectMatch 301 ^//(.*)$ http://example.com/$1
</IfModule>

<IfModule mod_rewrite.c>
    RewriteBase /

    # rule 1: remove multiple leading slashes (directly after the TLD)
    RewriteCond %{THE_REQUEST} \s/{2,}
    RewriteRule (.*) $1 [R=301,L]

    # rule 2: remove multiple slashes in the requested path
    RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
    RewriteRule (.*) %1/%2 [R=301,L]
</IfModule>

Header set X-Request_URI %{REQUEST_URI}e

You posted a screenshot where your ...app.com naked domain has a CNAME to another domain. Is the .htaccess file in that other domain?

If not, it won’t ever be triggered, and you’d need to seek help from whoever maintains that domain to verify that requests with double slashes are still arriving at the destination.

1 Like

I just opened the same page on that domain, and there is no new header in the response either.

However, can we switch our discussion to a _redirect file?
There are few redirects that work perfectly, but not:

// / 308

or

//:path /:path 308

For some reason, I got a redirect loop, but why?

It’s hard to troubleshoot when there are several layers of directives trying to do the same thing. URL Normalization is the first layer, it should happen before any Redirect Rules, and therefore your domain should be normalizing from example.com// to example.com/.

As for the _redirect file, are you referring to a list used with Bulk Redirects?

No, I’m talking about the file with the name - “_redirects”. More describe here - Redirects · Cloudflare Pages docs

Page Rules and Redirect Rules don’t work for me at all. Even with simple redirects, so that and the loop are probably similar problems :thinking:

Yes, it seems from past posts here that Pages will not respect Redirect Rules or even URL Normalization.

Have you tried?
/:path :path 308

Yep - didn’t work…