Pattern syntax in Apache, Cloudflare and Next.js
Apache
Apache's
Redirectdirective matches a URL prefix and appends the rest of the requested path to the target, so one line can move a whole directory.Apache's prefix matching works on whole path segments, so a rule for
/servicedoes not catch/servicefoo.txt.RedirectMatchis the regular-expression version ofRedirectin Apache. WithRedirectMatch, parts captured in parentheses are substituted into the target URL.For simple pattern redirects to another server, Apache prefers
RedirectorRedirectMatchovermod_rewrite.
Cloudflare
In Cloudflare, the parts matched by each wildcard can be reused in the target URL as ${1}, ${2} and so on. A Cloudflare redirect rule's wildcard pattern can include the hostname, as in Cloudflare's example http*://*.example.com/files/*. Cloudflare Single Redirects only work when Cloudflare proxies traffic for the requested hostname.
Next.js
In Next.js, adding * after a path parameter makes it match any number of nested path segments. For rules that need more than a pattern, see Next.js middleware and when to use it.
Wildcard DNS records
RFC 4592 defines a wildcard domain name by its leftmost label, the single asterisk (*) character. Cloudflare describes wildcard DNS records as a way to point hundreds or thousands of subdomains to the same resources. Cloudflare applies a wildcard record only when no exact record exists for the queried name. On Cloudflare, a *.example.com record is multi-level by default and covers abc.example.com and 123.abc.example.com. Cloudflare supports the wildcard only in the first label of a hostname.
Status defaults, query strings and the home-page trap
Each platform handles the status code differently. Set the status code on purpose.
Apache.
RedirectandRedirectMatchsend a temporary302unless a status such aspermanentor301is given.Cloudflare. Cloudflare wildcard redirects return a
301status by default. Cloudflare does not keep the query string on a wildcard redirect unless you turn that option on.Next.js. Next.js sends a
308status code (not301) for redirects marked permanent.
The home-page trap
Google's site-move advice covers many-to-one rules. Google advises against sending many old URLs to one unrelated URL such as the new home page. Google may report a catch-all redirect to an unrelated page as a soft 404. Google accepts redirecting many old URLs to one page when that page consolidates their content. To plan the redirect map for a whole site, see the best tools for a successful website migration.
How Pagepro checks redirect targets
Pagepro checks imported redirect targets against the new site's route table, not only the sources. A redirect that points to a path with no route ends in a 404.
Sources
- Single Redirects settingsCloudflare · developers.cloudflare.com
- Apache Module mod_aliasApache Software Foundation · httpd.apache.org
- Create a redirect rule in the dashboard - Cloudflare Rules docsCloudflare · developers.cloudflare.com
- Redirecting and Remapping with mod_rewriteApache Software Foundation · httpd.apache.org
- next.config.js: redirectsVercel (Next.js) · nextjs.org
- Site Moves and MigrationsGoogle · developers.google.com
- RFC 4592: The Role of Wildcards in the Domain Name SystemIETF · rfc-editor.org
- Wildcard DNS records - Cloudflare DNS docsCloudflare · developers.cloudflare.com