It’s late, and I’m tired, so this is gonna be a short post. I got a question from @Mattoid12 on twitter tonight about how to redirect a website. I’m going to show three ways, and then provide a link to a site which shows every conceivable way of redirecting a website.
.htaccess File
If you have access to edit your .htaccess file (FTP or SSH access to your server) put this line at the top. It will redirect the local url “/” (root) to the new location. More on HTTP 301 here. I’ve discussed this in more detail on a previous post.
Redirect 301 / http://yournewwebsite.net/
HTML Meta Tag
The example below will redirect a page to the new location after two seconds. This code should be placed at the top of your index or default page.
<meta http-equiv="refresh" content="2;url=http://yournewwebsite.net">
PHP Redirect
This method is sort of a combination of the above two approaches. It will perform an instant redirect and return HTTP 301 to the browser. This code needs to be entered in your index.php file.
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://yournewwebsite.net" );
?>
For examples of a large number of other possible redirects are available on webconfs.com.
Tags: .htaccess · change · configuration · html · http · meta · php · redirect · repoint · web · web server · website3 Comments
Nice post dude, I like the PHP solution too, good work on a clean way to do the PHP Redirect.
What you should do for a follow up post is 302 redirects and when to use them appropriately.
Good idea. HTTP protocols are so misunderstood… Like the kid in the black trenchcoat sitting at the back of the classroom.
Awesome, thanks, I just used your 301 redirect in my htaccess and it worked perfectly!