When a page moves or is replaced, a redirect can send visitors and search engines from the old URL to the correct destination. This guide explains the difference between Apache .htaccess and Nginx redirect rules, with examples for each server type.
A permanent (301) redirect sends visitors and search engines from an old URL to its replacement. The right way to configure it depends on your web server: Apache can use .htaccess, while Nginx does not read .htaccess files. Check which server your host uses before applying the examples below.
How to Add a 301 Redirect in WordPress
Apache: use .htaccess
Use this method only if your website is served by Apache and .htaccess overrides are enabled. Back up the file before editing it. Add the rule outside any existing WordPress-managed block, replacing the example paths with your real URLs:
Redirect 301 /oldpath https://www.example.com/newpath/
The source path begins with a slash and is relative to the site root. The destination is the full URL. Add a separate rule for each old URL you need to redirect. If your site uses Nginx, do not add this rule to .htaccess; use the Nginx approach below instead.
Nginx: configure the server block
Nginx ignores .htaccess files. A server administrator can add a redirect in the appropriate HTTPS server block, then test and reload the configuration. For one exact path, the rule can look like this:
location = /oldpath {
return 301 https://www.example.com/newpath/;
}
Place the location block inside the relevant server block, not inside another location block. Test the Nginx configuration before reloading it. If you use managed hosting, ask your host to add the rule or use its supported redirect tool.
How 301 Redirects Work
A 301 status code tells browsers and search engines that a URL has moved permanently. A 302 redirect is generally used when the move is temporary. Choose the status that matches your actual situation; do not use a permanent redirect simply to test a destination. For multiple old URLs, add a specific rule for each path and point it to the closest relevant replacement.
In the examples, /oldpath is the old path on your site and https://www.example.com/newpath/ is the destination. The old path can include a file extension, such as /oldpage.html, if that is the URL being retired. Redirecting to another domain is technically possible, but for SEO and usability, send visitors to a genuinely relevant replacement rather than an unrelated page.
How to Test a 301 Redirect
After adding the rule, test the old URL and confirm that it returns an HTTP 301 status with the expected destination in the Location response header. You can check this from a terminal with:
curl -I https://www.example.com/oldpath
Look for a response similar to HTTP/2 301 and a location: header containing the replacement URL. If you see a 200 response, the rule may not be loading. A 404 can mean the source path does not match, while an unexpected destination may indicate another redirect rule or a redirect chain.
To inspect multiple redirects, use curl -IL with the old URL. Ideally, the old URL should reach its final destination in one redirect. Clear or bypass browser and page-cache layers when testing, because browsers and caching systems may retain permanent redirects.
Important Redirect Tips
- Check Apache matching behavior: The Apache
Redirectdirective matches the specified path and paths beneath it. For an exact-path-only match, useRedirectMatchwith an anchored regular expression. - Avoid redirect loops: Make sure the destination does not redirect back to the original URL, directly or through another rule.
- Update internal links: When possible, change menus, links, canonical URLs, and XML sitemaps to point directly to the new URL instead of relying on a redirect.
- Keep the replacement relevant: Redirect retired pages to the closest useful equivalent. Do not send unrelated URLs to the homepage just to avoid a 404.
- Use 301 only for a permanent move: If you are testing a rule or expect the move to be temporary, use a temporary redirect until the destination is confirmed.
