要实现301重定向,可以通过修改.htaccess文件或在PHP代码中使用header()函数。下面是两种具体的实现方法:
- 在.htaccess文件中进行301重定向
在你的网站根目录下的.htaccess文件中添加以下代码:
RewriteEngine on
RewriteRule ^old-url$ http://www.yourdomain.com/new-url [R=301,L]
其中,“old-url”为需要被重定向的旧网址,“http://www.yourdomain.com/new-url”为新网址。
- 在PHP代码中使用header()函数进行301重定向
在你的PHP代码中使用以下代码:
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.yourdomain.com/new-url");
exit();
同样的,“http://www.yourdomain.com/new-url”为新网址。这种方法需要确保在header()函数之前没有任何输出。