Redirects in a nutshell
The name ‘redirect’ pretty much says it all: It sends visitors traveling to a specific page to an alternative one. But what does this 301 mean and how does it differ from a 302 redirect? Both send your users to a different page. The only subtle (yet very important) difference is that a 301 will permanently send visitors and search engines to the new destination. 302 redirects indicate that you only temporarily want visitors to be sent to a different page.
Creating a 301 redirect on the server
One of the most basic methods of adding a 301 redirect, is by editing your .htaccess file on the server. This method is only available on Apache servers. Nginx has their own way of defining redirects in the server configuration and requires extensive knowledge of system administration.
These configurations can get quite unmaintainable over time, especially if you’re an avid blogger or you’re trying to improve the SEO of your posts. On top of that, you would have to log in on your server over FTP, edit the files and re-upload them every time you add a new redirect. That’s why, generally speaking, using this method is not considered the way to go.
Creating a 301 redirect with PHP
As a WordPress developer, you have two options: Either you make a redirect by altering the headers of a file in the code -or- you make use of WordPress’ built-in
An example of plain PHP could be as follows:
<?php // MyExampleFile.php header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.my-blog.com/a-new-destination"); ?>
And this is how you’d do the same, but now by using WordPress’ built-in function:
wp_redirect( "http://www.my-blog.com/a-new-destination", 301 );
If you forget to add the
301, both WordPress and PHP will both assume that it’s a
302 redirect, which isn’t always the case.
This method is a bit easier than editing files on the server, but can also become cumbersome once the amount of redirects increases.