How to redirect one page to another
Where do I even put a redirect?
It depends on how your site is run, and that's the first thing to find out. Google says the way you set up a server-side redirect depends on your hosting and server, or the scripting language behind your site.1
If you can't tell which you have, ask your host. Google's advice for servers it doesn't cover is to check with your server manager or hoster.1
What if my site is on a hosted platform?
Look in its help pages first. Google's example platforms, Blogger and Shopify, may come with redirect tools already built in. Its suggestion is to search for a help article.1
How do I do it on Apache?
Apache is a web server program. Its Redirect directive maps an old address to a new one by asking the client to refetch the page at the new location.2 (A directive is one line of configuration.) It can go in the server config, a virtual host, a directory section or an .htaccess file.2 That last one is a small settings file in a site's folder. MDN also notes redirects can be set in the main server config file or in a directory's .htaccess.3
The pattern looks like this.
- Temporary, the default:
Redirect "/old-page" "https://example.com/new-page". If no status is given, Apache sends a temporary redirect (status 302).2 - Permanent:
Redirect permanent "/old-page" "https://example.com/new-page". Thepermanentkeyword returns a permanent redirect (status 301).2
The old path has to begin with a slash, and a relative path isn't allowed.2 One more thing to watch: only complete path segments are matched. So a rule for /service won't catch /servicefoo.txt.2
Follow Apache's own documentation for your version, and keep a copy of the file before you edit it.
How do I do it on nginx?
In nginx, one way is a return line. The documentation says return can take a redirect address for the codes 301, 302, 303, 307 and 308.4 A permanent one for a single page looks like return 301 https://example.com/new-page;, and the temporary version swaps in 302.4
For pattern-based rules, nginx has rewrite. Pick the flag redirect for a temporary redirect (302) or permanent for a permanent one (301).4 The 301 and 302 page explains how to choose. MDN's guide to common servers shows nginx redirects built in a separate server block for the content you want to redirect.3
How do I do it in PHP?
If your site runs PHP, you can send the redirect from the script. Google says to use the header() function to set a permanent redirect, and the headers must be set before anything is sent to the screen.1 You'd send a status line and a Location line, then stop the script. Google's page has the exact lines to copy.1
How do I know it worked?
Don't trust the rule. Test the result. To see the status code and the final address, follow the steps in this checking guide. If a new rule clashes with an old one, the page on chains and loops helps.
Cited material
- Redirects and Google Search, Google Search Central
- Apache module mod_alias, Apache HTTP Server Project
- Redirections in HTTP, MDN Web Docs (Mozilla)
- Module ngx_http_rewrite_module, nginx