Regex Rules

What is the name of the domain?

example.com

What is the issue you’re encountering

Cannot setup regex or not know how

What steps have you taken to resolve the issue?

We need to setup a rule ( transform or page rule ) with regex I think

Old URL

https://www.example.com/showthread.php?2875401-why-dont-the-old-showthread-urls-auto-redirect-to-the-new-community-threads-urls

New URL

https://www.example.com/community/threads/why-dont-the-old-showthread-urls-auto-redirect-to-the-new-community-threads-urls.2875401

So looking at the above, we need showthread.php? to be /community/threads. AND the number at the end of php? need to be appended to the end of the URL. Bear in mind that this number will change depending on the thread so whatever that number of it needs to be put at the end.

Further, we need it for usernames too.

Old URL
https://www.example.com/member.php?638048-testuser

New URL we need all usernames after the member.php?638048-testuser

Redirect to new URL

https://www.example.com/community/members/testuser.638048/

So similar to the above we need any request going to member.php?638048-testuser to be /community/members/testuser.638048/
Also as these are usernames there will be many so any request for member.php… redirects to /community/members/…/

We have a business account ( ive already lodged a ticket ) so have more rules than pro. I hope all that makes sense.

Thank you.

It’s not clear what the issue is. Are you asking for an escalation on the ticket? Is something broken?

Hello.

No I am simply asking if any of the community could assist as it is been day two with no reply.

I hope the initial issue made sense. :slight_smile:

Please don’t lodge tickets with Support if nothing’s broken. There are enough people complaining about the backlog as it is.

Rather than RegEx, you might want to try using Wildcards:

Im sorry but this is why we pay for a business plan.

Business plan

Designed for business-critical websites and applications. It gives you the highest level of control and customization of all self-serve plans, plus 100% uptime SLA and prioritized ticket support.

If you need solutions engineering, that’s for Enterprise customers. Tickets are for troubleshooting:

Documentation and Community are for helping users learn how to use Cloudflare, which is why I recommended the Wildcard approach, and provided a link to the documentation.

If you need help writing RegEx, I recommend trying ChatGPT, or similar. Your examples look to be pretty straightforward, and I’ve found ChatGPT to be quick and easy for this. Here’s more info on regex_replace for a Dynamic Single Redirect:

https://developers.cloudflare.com/ruleset-engine/rules-language/functions/#regex_replace

I also run my RegEx through regex101.com to test regex_replace.

OK cheers. Thanks for your guideance. :slight_smile:

2 Likes

We couldnt get it to work with redirect so we use Cloudflare Workers which works .

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  let url = new URL(request.url)

  // Check if the URL matches the old pattern
  if (url.pathname.startsWith("/showthread.php")) {
    let query = url.searchParams
    let fullQuery = query.toString() // Full query string (e.g., 2875401-why-dont-the-old...)

    // Extract thread ID and title from the query string
    let match = fullQuery.match(/^(\d+)-(.*)$/)
    if (match) {
      let threadId = match[1] // Capture group 1: Thread ID
      let threadTitle = match[2] // Capture group 2: Thread title

      // Construct the new URL
      let newUrl = `https://www.example.com/community/threads/${threadTitle}.${threadId}`

      // Redirect to the new URL
      return Response.redirect(newUrl, 301)
    }
  }

  // If no match, return original request
  return fetch(request)
}

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.

For straightforward use cases like this, I’d recommend using Snippets. These are provided at no cost for Pro users and above. :wink: