Cloudflare Pages: redirect based on query string

Hi, i am moving a website from wordpress to hugo, but i am facing a problem with permalinks like this /?p=285 they are converted to just p285 in hugo, i tried using redirects with htaccess but it didnt work, i hosting the site in cloudflare pages

Your approach doesn’t make sense to me, as all of these point to the same published file:

/?p=6
/?p=7
/?p=42

They all point to the home page.

in my front matter i have this permalink: ‘/?p=143’ but when i build the site those special characters are removed that why i want to use redirects i want the people that come from google using the old wp permalinks to be redirected to the new ones,

I understand what you want to do, but you need to do it a different way: rewrite the URL. With Apache2 you would use mod-rewrite. Netlify can handle this, so check the Cloudflare docs.

Revisiting your original question, do all of your canonical URLs use this pattern?

https://www.example.org/?p=285

If yes, why? Normally you would configure WP permalinks to something other than “plain” (e.g., post title).

Hey, yes all the urls are like that, the problem here is they are indexed in google if they weren’t i would have just used the new urls without the special characters, i’ve read your previous comment, and i don’t use netlify, and for cloudflare i found in the docs that they use a file ( _redirects ) and i tried that but it stills not working, and about mod-rewrite dont we need apache for that ?

How many pages need to be redirected? This will determine which approach to take to avoid exceeding Cloudflare’s redirect limits.

13 / 14 pages, not too many

Cloudflare Pages does not provide a simple mechanism to perform redirects like these:

Request URL Target URL
https://example.org/?p=6 https://example.org/something/
https://example.org/?p=7 https://example.org/something/else/

But you can handle this by combining a redirect rule with a redirects file. The redirects file is limited to 2000 entries.

You will end up with a double-hop, but both are 301 redirects so you shouldn’t see any ranking changes. Over time the search engines will begin using the new canonical URLs.

You can test this approach using this live site hosted on Cloudflare Pages:
https://www.boldbeam.com/other-tests/redirects/

The project repository is here:
https://github.com/jmooring/hosting-cloudflare-pages

Files of interest:

1 Like

If your domain is connected to Cloudflare (which is likely the case), you have a setting under:

Rules → Redirect Rules → Create new Single Redirect

Create the following setting:

Wildcard pattern

https://domain.com/?p=*
https://domain.com/${1}/

And now, in your old posts .md files, specify the setting in the front matter:

aliases = ["/33/"]

Now if Cloudflare redirects all addresses with ?p=* initially to /*/ , then Hugo can catch such IDs using aliases and perform an HTML redirect to the /normal-address/.

It’s a bit of a workaround solution, but you won’t have to add them in bulk to Cloudflare.

1 Like

Hey, whats the 33 in aliases stand for, should i add that to every post ?

That’s what I proposed above, but using the aliases field to build a redirects file instead of alias files.

1 Like

33 - its example id for your old post ?p=33

In your old posts .md files, you create an aliases field where the corresponding post ID is specified. For example, in hello-world.md , you would set aliases = ["/1/"] and in first-post.md , it would be aliases = ["/2/"] , and so on.

hello-world.md

---
title: "Hello World"
date: 2023-10-20
aliases: ["/1/"]
---
text

first-post.md

---
title: "First Post"
date: 2023-10-21
aliases: ["/2/"]
---

text

using @icedogas approach, when i visit domain.com / ?p=1 it redirect me to domain.com/1 while the url i have is domain.com/p1

Did you take the time to read this?
https://www.boldbeam.com/other-tests/redirects/

And test the links at the bottom of the page?


Are you sure everything is set up correctly? Here’s an example of the settings.

1 Like

i just read your reply about the post id, i am so dumb i kept it as it is without change in it, now its working, thank you very much

1 Like

Hey, i just checked that page out, and i will test your approach in my next wordpress site that i am converting to hugo, thank you so much for taking the time to help me. I really appreciate it!

Alias files cause a client side redirect. Use a _redirects file for server side redirects. In addition to being snappier, you’re not littering your public directory with a bunch of alias files.

Hi, i have a question, i am setting up a new repo to test your approach, does this works on cloudflare pages subdomains or just custom domains ( first level )