首页> 实战笔录 >PHP开发笔记 >ThinkPHP ThinkPHP
url的301跳转 tp3.2 tp5.0实现方式
作者:小萝卜 2019-08-21 【 TP5 TP3.2 】 浏览 4405
简介ThinkPHP怎么配置url的301跳转,永久转移
ThinkPHP怎么配置url的301跳转,永久转移
很简单,只需要在目录下的.htaccess文件里面添加
第一种情况,是将整个网站所有地址都做301跳转
RewriteCond %{http_host} ^luowebs.com [NC]
RewriteRule ^(.*)$ http://www.luowebs.com/$1 [L,R=301]
第二种情况,是网站特定的几个链接做301跳转
#以前的页面链接是:A
http://www.wolfcode.cn/newsWeb/newsDetail/1246.html
#现在的页面链接是:B
http://www.wolfcode.cn/article/index/id/526
#我们要由A重定向到B;那么我们的规则就是:
RewriteRule (.*)/article/index/id/526 http://www.wolfcode.cn/newsWeb/newsDetail/1246.html [L,R=301]
301官方解释:(永久移动)请求的网页已永久移动到新位置。服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将请求者转到新位置。您应使用此代码告诉 Googlebot 某个网页或网站已永久移动到新位置。
注意:不要在控制器用header去跳转
if($_SERVER['SERVER_NAME']=='luowebs.com'){
header('Location: http://www.luowebs.com'.$_SERVER['REQUEST_URI']);
exit();
}
上面代码使用header做的跳转,这里同学们需要注意以下了,这代码这么写虽然能跳转,但是你去检测会发现这是302跳转不是301跳转哦,大家千万要注意啦!
在tp5.0中可以直接使用redirect来做301跳转:
//重定向到指定的URL地址 并且使用301
$this->redirect('http://www.luowebs.cn',301);
//重定向到指定的URL地址 并且使用302
$this->redirect('http://www.luowebs.com',302);
手册地址:https://www.kancloud.cn/manual/thinkphp5/118051
很赞哦! (0)
相关文章
- ThinkPHP3.2和5.0的区别
- Tp6 $request->isAjax()不能验证axios提交的请求
- TP6/ThinPHP6 获取主键自增ID
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Attr_id' in 'where clause'
- TP6报错:Non-static method think\Request::controller() should not be called statically
- TP6 databackup拓展插件,实现数据库备份下载还原代码分享
- PHPExcel在TP3.2下使用方法
- TP6/ThinkPHP6 多应用模式域名串模块的问题
- tp5/tp6新增app版本跟新接口
- tp5使用jwt生成token,做api的用户认证