Taking .php out of page addresses without breaking old links
phpurlsredirectsapachefallbackresourcerewriteroutingservice-worker
A site made of plain PHP files shows its file names in every address: /watch.php?v=cat, /doc.php?t=abc. Clean addresses (/watch/cat, /doc/abc) are shorter to share and no longer tie the URL to the implementation. The work is small, but the old addresses are already in texts, emails and bookmarks, so the change is mostly about keeping them alive. Done on two sites in 2026-11 (a video site and a quoting app), with two different routing methods.
Decide what is worth changing
Only addresses a person sees or shares matter: pages, sign-in, links sent to customers. Endpoints that only scripts fetch (api.php, upload handlers, form posts) gain nothing and can break things:
- an endpoint baked into a released app cannot move without an app release;
- an endpoint with its own server rule (an nginx
location = /upload-part.phpraising the body size limit) silently loses that rule under a new name.
Leave those alone on purpose and write down that it was on purpose, so nobody "finishes the job" later.
Routing: two ways
- A fallback script. With
FallbackResource /router.phpin the Apache vhost, any path that matches no real file goes to one script, which looks at the path and includes the right page. If the site already has such a fallback (the video site's series pages), the new routes need no server change at all. Real files always win, so static assets can never be shadowed. - Rewrite rules. One
RewriteRuleper page in the vhost (^/doc/([A-Za-z0-9]+)$to/doc.php?t=$1). Fine for two or three pages; it is a server config change, so back up the vhost and runapachectl configtestfirst (Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)).
Either way the page files stay where they are. That is what makes the redirect step easy.
Retire the old address from inside the page
Each page file starts with one helper call that knows its clean address:
- A GET or HEAD for the file-name form gets a 301 to the clean form, query string preserved and reshaped as needed (
watch.php?v=x&t=30becomes/watch/x?t=30). - A POST is answered in place, not redirected. A sign-in form left open in a tab across the deploy still posts to
login.php, and a 301 would turn that POST into a GET and lose the form. - Arriving through the router (the clean form) does nothing.
One function, called first thing in every page, also means a new page cannot forget its redirect.
Things that break quietly
- Relative links change meaning. A page served at
/doc/<token>is one directory deeper than/doc.php, sohref="icon.png"now points at/doc/icon.png. Either use root-relative links, or have the helper return the../prefix the page needs (which keeps an install under a subpath working, as does a relativeLocationin the redirect). - Reserved names. If user-chosen slugs share the namespace (
/<series-name>), the new page names must be refused as slugs:watch,login,logout,admin,api,assetsand so on. Check the existing data for collisions before shipping and have the create and rename calls answer 409. - Service workers. A worker that skipped caching by testing for
.phpin the URL will now cache the clean pages, including private ones. Exclude the new paths explicitly and bump the cache name. - PHP's built-in dev server has no vhost, so
php -S 127.0.0.1:8790 -t public404s every clean address. Give it a router script that mirrors the server rules:php -S 127.0.0.1:8790 -t public tools/router.php. The reverse mistake also bites: a stray router argument on a site that does not need one makes every request fail. - Links built in JavaScript (the address-bar swap of an in-page player, share buttons) are easy to miss; grep the scripts for
.phptoo. Build links through one function (watch_path($slug)) so the next change is one edit.
Testing
Write the address checks as a script that takes a base URL: every clean page answers 200, every old form answers 301 with the right Location and query, no served page still contains a .php link, unknown paths 404. Run it against the local dev server before the deploy and read-only against the live site after. When verifying a customer-facing link live, a made-up token that renders the "not available" page proves the routing without putting test data in the production database.
See also
- Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)
- videos.jhampton.io (private video site architecture)
- "Anyone with the link" share links (capability URLs)
- Moving a website to a new server without downtime