The rewrite that replaces an application in one release has a long history of failure. It runs late, it misses behavior the old system had, and on cutover day everything changes at once. The approach that works is older and less exciting: put the new system in front of the old one, and move traffic across a piece at a time until there is nothing left behind it.
This is how we run that approach when the destination is Next.js.
Step 1: put Next.js in front
The first release changes nothing users can see. Next.js is deployed in front of the existing PHP application, and every request it does not handle itself is passed through to PHP unchanged.
Next.js supports this directly. Rewrites configured with a fallback are checked
only after Next.js has looked for its own pages, so any path without a Next.js
page goes to the legacy origin:
// next.config.js
module.exports = {
async rewrites() {
return {
fallback: [{ source: '/:path*', destination: 'https://legacy.internal/:path*' }],
};
},
};
At this point the whole site still comes from PHP, through Next.js. Measure it. Latency, error rates and cache behavior should match the old setup before any route moves.
Step 2: choose the first routes carefully
The first routes to move should be valuable enough to matter and simple enough to get right: a marketing page, a content section, a read-only listing. Avoid checkout, login and anything that writes data until the pattern is proven.
For each route:
- Build the page in Next.js, reading data from the existing back end. That is often the PHP application itself, exposed through a small JSON endpoint.
- Compare old and new in production. Render both, diff the content, check the structured data, and compare performance.
- Switch the route behind a flag, so reverting it is a configuration change, not a deploy.
Step 3: share identity across both stacks
Once users can be logged in on pages from both systems, sessions have to work across the boundary. There are three common patterns:
- Shared cookie, PHP as the source of truth. Next.js forwards the session cookie to a PHP endpoint that answers "who is this?". Simple, and it keeps authentication where it already works.
- Shared session store. Both stacks read the same Redis or database session. Faster, but it needs the PHP session format understood on both sides.
- Move to tokens. Introduce an identity provider and signed tokens both sides verify. The right long-term answer, and best done as its own step.
Pick one deliberately. Authentication bugs during a migration are the kind that make the news.
Step 4: keep search equity
A public site's URLs carry years of rankings. Moving a route must not change what search engines see unless the change is deliberate:
- Keep the URL, or redirect the old one with a permanent redirect.
- Carry over titles, meta descriptions, canonical tags and structured data.
- Keep internal links pointing at final URLs, not at redirects.
- Watch crawl errors and rankings for every batch of routes, and pause if they move.
Step 5: keep the old system safe
A migration of any size runs for months. The PHP that still serves traffic during that time needs security fixes like any production system. If the runtime is past end of life, cover it with long-term support for the length of the project. The migration should be a choice about the future, not a deadline set by the next vulnerability.
Step 6: turn the last piece off
The end of a strangler migration is undramatic. When the last route has moved and nothing calls the PHP back end directly, the fallback rewrite is removed and the legacy servers are retired. Keep a backup of the code and data, write down what was retired and when, and close the project.
Where this does not fit
The approach needs a request boundary to work at: pages, routes, endpoints. A batch system, a desktop-style application or a back end that is all business logic is better upgraded than migrated. Deciding that honestly, before any work starts, is part of the job.