Renaming /tag/* to /tags/* should not require changing the main application or breaking old bookmarks. A small Worker can intercept the old path and fetch the new one while preserving the request method, headers, and query string.
The Worker
export default {
async fetch(request) {
const url = new URL(request.url);
// Only handle /tag/*, leave /tags/* and everything else alone
if (url.pathname.startsWith("/tag/")) {
url.pathname = url.pathname.replace(/^\/tag\//, "/tags/");
return fetch(url.toString(), request);
}
return fetch(request);
},
};
The path check leaves every other request alone. For /tag/*, the regular expression swaps only the first path segment before passing the original request options along.
Create the Worker
In the Cloudflare dashboard, go to Workers & Pages -> Create application -> Create Worker. Open the Worker, pick Edit code, paste the script, then Save and deploy.
Attach a Route
In the Worker, go to Settings -> Domains & Routes -> Add -> Route. Pick the zone example.com and set the route to example.com/tag/* so it only rewrites under /tag/*.
DNS and Proxy Prerequisites
Routes require proxied DNS Your domain needs an orange-cloud proxied DNS record in Cloudflare. Without it, requests bypass Cloudflare entirely and the Worker never runs.
Patterns and Precedence
A rewrite leaves the address unchanged This Worker does an internal rewrite, not an HTTP redirect. The user’s URL bar still shows
/tag/. If you want a 301 redirect for SEO, returnResponse.redirect()instead offetch().
Route patterns work with wildcards at the start of hostnames or end of paths, like example.com/tag/*. Putting wildcards mid-path (example.com/*/tag/*) is invalid. Specific routes win over general ones.
Optional: Broader Coverage
Set the route to example.com/* and keep the code check so it only rewrites /tag/*. Other paths pass through, and the Worker runs more often but you manage fewer routes.
Quick Edits in the Dashboard
For small tweaks, use the dashboard’s Quick Edit (VS Code in the browser) to change code, preview, and redeploy without local tools. Save Wrangler for CI/CD or bigger projects.
Verification
Open https://example.com/tag/some-topic?source=test. The page should contain the content from /tags/some-topic, the query string should still reach the application, and the address bar should continue to show /tag/ because this is a rewrite rather than a redirect.
That distinction is the whole reason to keep this Worker small. The old URL continues to work; the application only has to understand the new one.
Was this useful?
Thanks for the feedback.