How to Set Up a 301 Redirect
Point an old URL permanently at a new one with a 301 redirect in Apache, nginx, Next.js, or Vercel, then confirm it works with curl.
A page moved, or two URLs serve the same content and one has to go. A 301 redirect sends visitors and search engines from the old address to the new one and passes the old page's ranking along. Where you add it depends on what serves the site.
Add the redirect in Apache
Apache reads redirects from a .htaccess file in the site root. Add one line per redirect:
Redirect 301 /old-page /new-page
The first path is the old URL, the second is the destination. The destination can also be a full URL such as https://yourdomain.com/new-page. Save the file; Apache applies it on the next request.
Add the redirect in nginx
nginx does not read .htaccess. Add a location block inside the server block of the site config, usually under /etc/nginx/sites-available/:
location = /old-page {
return 301 /new-page;
}
The = matches that exact path only. Run sudo nginx -t to check the syntax, then sudo systemctl reload nginx.
Add the redirect in Next.js or Vercel
For a Next.js site, export a redirects() function from next.config.js:
module.exports = {
async redirects() {
return [
{ source: '/old-page', destination: '/new-page', permanent: true },
];
},
};
For any site hosted on Vercel, put the same rule in vercel.json at the project root:
{
"redirects": [
{ "source": "/old-page", "destination": "/new-page", "permanent": true }
]
}
permanent: true produces a 308 status, which browsers and Google treat the same way as a 301. Deploy for the change to take effect.
Test it with curl
curl -I https://yourdomain.com/old-page
The first line should read HTTP/2 301 (or 308), and a location: header should show the new URL. A 200 means the rule is not being applied. If the output shows one redirect leading to another, point the old URL directly at the final destination.
301 versus 302. A 301 is permanent and passes ranking signals to the new URL. A 302 is temporary and tells search engines to keep the old URL indexed. Use 302 only for changes you intend to reverse.
Browsers cache 301s. Once a browser has seen a 301 it stops asking the server and goes straight to the destination. If the target was wrong, fixing the rule will not fix it for people who already visited, so test with curl or a private window before announcing the new URL.
More Websites how-tos
- How to Add a Favicon to a Website
- How to Add a Website to Bing Webmaster Tools
- How to Add a Website to Google Search Console
- How to Add Alt Text to Images
- How to Add Google Analytics to a Website
- How to Add Open Graph Tags to a Website
- How to Add Structured Data to a Web Page
- How to Check DNS Propagation
- How to Check If a Website Is Indexed by Google
- How to Compress Images for a Website
- How to Connect a Domain to Vercel
- How to Create a Sitemap for a Website
- How to Make a Website Mobile-Friendly
- How to Move a Domain to Cloudflare DNS
- How to Redirect www to non-www
- How to Set a Canonical URL
- How to Set Up Email Forwarding for a Domain
- How to Write a Meta Description
- How to Write a robots.txt File