TL;DR
- Why 301 Redirects are Essential
- Implementing Redirects via .htaccess
- PHP-Based Redirects in WordPress
Why 301 Redirects are Essential
A 301 redirect tells search engines that a page has permanently moved to a new location. Without them, users hitting an old URL see a 404 error, and search engines lose the "link juice" accumulated over time. When done correctly, a 301 redirect passes nearly 100% of the original page's authority to the new one.
Implementing Redirects via .htaccess
For Apache servers, the .htaccess file is the most efficient place to manage redirects. It happens before WordPress even initializes, making it lightning-fast.
# Redirect a single page
Redirect 301 /old-page https://yoursite.com/new-page
# Redirect an entire site to a new domain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
Pro Tip: Use the 301 Redirect Rule Generator to build complex regex rules without the headache.
PHP-Based Redirects in WordPress
Sometimes you need dynamic logic for a redirect. In these cases, using WordPress's wp_redirect() function is appropriate.
add_action('template_redirect', function() {
if (is_page('old-slug')) {
wp_redirect('https://yoursite.com/new-slug', 301);
exit;
}
});
Frequently Asked Questions
What is the difference between 301 and 302 redirects?
Should I use a plugin for redirects in WordPress?
Can a 301 redirect hurt my SEO?
Key Takeaways
- Why 301 Redirects are Essential: Practical action you can apply now.
- Implementing Redirects via .htaccess: Practical action you can apply now.
- PHP-Based Redirects in WordPress: Practical action you can apply now.