url优化可是网站开发的必备高阶技能,先看本实例优化前后效果比较:
(同为调用前台模块下的index控制器下的index方法)
优化前:www.yxxxm.com/index.php/index/index/index。
优化后:www.yxxxm.com/xx/id/shtml。
[ Apache ]
httpd.conf配置文件中加载了mod_rewrite.so模块
AllowOverride None 将None改为 All
把下面的内容保存为.htaccess文件放到应用入口文件的同级目录下
Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
默认的tp的.htaccess 和就多了一个 '?' 而已~
将index.php放到根目录并对他进行修改
// 定义应用目录
define('APP_PATH', __DIR__ . '/application/');
// 定义版本号
define('VERSION', '1.1.3');
//重定义扩展类库目录
define('EXTEND_PATH', __DIR__ . '/extend/');
//重定义第三方类库目录
define('VENDOR_PATH', __DIR__ . '/vendor/');
//绑定前台目录
define('BIND_MODULE','index');
// 加载框架引导文件
require __DIR__ . '/thinkphp/start.php';
也就是去掉 '..'这些而已
我推荐使用批量注册的方法绑定:
目标文件:application/route.php
先注释掉文件内的原有代码然后写入如下代码:
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st
// +----------------------------------------------------------------------
/*
return [
'__pattern__' => [
'name' => '\w+',
],
'[hello]' => [
':id' => ['index/hello', ['method' => 'get'], ['id' => '\d+']],
':name' => ['index/hello', ['method' => 'post']],
],
];*/
//引入系统类
use think\Route;
//定义路由规则
Route::rule([
'index'=>'index/index',
'/about'=>'index/about',
'/news'=>'index/news',
'/products'=>'index/service',
'/message'=>'index/customer',
'/contact'=>'index/contact',
如果隐藏id的话
'/contact/:id'=>'index/contact',
],'','get|post');
3.改完配置后,重启apache
五、完全Ok啦,使用新入口吧
Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?s=$1 [QSA,PT,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yxxxm.com/$1 [R,L]