How To 301 Redirect With PHP
Search engines index pages by the URL of the page, if these pages are moved to a different URL then the search engines will have indexed a page that no longer exists. Therefore people search in the search engine and find your people, click through to your website and they will get a 404 message. This is not only a bad user experience but also bad SEO, search engines do not want to index 404 pages.
If you move an index page to a different URL you need to make sure you do a 301 redirect to the correct page. A 301 redirect will tell the search engines that this page has been permanently moved to a new location and to change the index to the new URL.
You can easily do this in PHP by changing the HTTP header.
Copy the following snippet to the top of the moved page to 301 it to the new location.
<?php Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: http://newurlhere.com" ); exit; ?>
Learn how to code with Treehouse
- Learn projects with access to 1000+ videos
- Practice live with our Code Challenge Engine
- Get help in our members-only forums
- Access 100s of premium tutorials and downloadable content
- Members content consists of premium WordPress plugins
- CSS packages, jQuery packages, tutorial demo files and templates for 100s of web development tutorials
- In-depth development tutorials
- Priority tutorial requests
- No ads
- First month free!