301 Redirect

Scribbller 0 Tallied Votes 96 Views Share

A server side generated 301 redirect for a permenantly moved URL

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.com/");
exit();
?>
dap62 0 Newbie Poster

That is one way to do it, and it does work. It does require that you have the redirection php file on the same path, and the same file name as the previous file. However, if the previous file had a file name extension other than a php file extension, it won't work (unless you use some additional tricks).

If you are using Apache for your HTTP server, there is an easier solution. You can put the following line in the .htaccess file in the website instead.

RedirectMatch permanent ^/path/old_filename.htm$ http://www.domainname.com/path/new_filename.htm

Where "/path/" would be the old directory path to the old file from html_public root, and "old_filename.htm" is the name of the old file that no longer exists on the web server. The new target file would be specified with ""http://www.domainname.com/path/new_filename.htm". Including the keyword "permanent" will cause a 301 status code to be returned with the redirection, as opposed to "temporary" which would return a 302 with indicates a temporary URL change.

Don

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.