301 redirect htaccess and php 301 redirect

If you need to change the URL of a page as it is shown in search engine results, we recommend that you use a server-side 301 redirect.

Good Web Hosting

Source: https://support.google.com/webmasters/answer/93633?hl=en

It is friendly to seo. This is the best way to ensure that users and search engines are directed to the correct page.

The 301 status code means that a page has permanently moved to a new location. In this post, I will share some 301 redirect code, include:htaccess 301 redirect and php 301 redirect.

PHP Single Page Redirect

header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.yourdomain.com/page.html");

PHP Canonical Redirect

if (substr($_SERVER['HTTP_HOST'],0,3) != 'www') {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.'.$_SERVER['HTTP_HOST']

Apache .htaccess Singe Page Redirect

Redirect 301 /old/oldpage.htm /new/http://www.domain.com/newpage.htm
Apache .htaccess Canonical Redirect
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

ASP Single Page Redirect

< %
Response.Status="301 Moved Permanently"

ASP Canonical Redirect

< %
If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www."
& Request.ServerVariables("HTTP_HOST")
& Request.ServerVariables("SCRIPT_NAME")
End if