我们在搭建部分thinkphp框架的网站的时候,要求绑定的网站的目录是/public/目录,而不是根目录。
如果是宝塔服务器里面还好,可以直接设置网站运行目录,或者把域名绑定到二级目录里面。
但是虚拟主机,只能绑定根目录是web目录,没有选择,这时候,我们就用到使用.htaccess来绑定/public/了。
Apache
方法:在根目录下面新建.htaccess,把下面的内容,复制进去,即可访问。
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1?Rewrite [L,QSA]
</IfModule>
nginx
根目录下新建.rewrite.conf文件
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
TP6 部署到二级目录访问设置方法
二级目录伪静态,我以en目录为例,使用时把en目录换成你自己的目录名。
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
#根目录下en目录下面的伪静态规则
location /en/ {
if (!-e $request_filename){
rewrite ^/en/(.*)$ /en/index.php?s=$1 last; break;
}
}
进阶:去掉一些默认的地址
例子: 原链接 https://www.01j.top/index.php/home/Goods/index.html?goods_id=1
配置后链接https://www.01j.top/product-1.html
配置Nginx文件
//为了把访问路径带了index.php 和 home应用目录去掉,并且不影响原有访问方式
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^/admin/(.*)$ /index.php?s=/admin/$1; //后台应用
rewrite ^((?!/home).*)$ /index.php?s=/home/$1; ##隐藏默认模块名 /home
rewrite ^/(.*)$ /index.php?s=$1 last;
break;
}
#autoindex on;
}
在home应用目录文件夹下添加route/app.php,文件内容如下
设置home/goods/index 控制器url访问方式为product,pattern定义参数
use think\facade\Route;
//说明 :cate_id 表示只能带/参数 <b_id?>表示可选参数
//-?表示可选符号 -<cate_id> 表示可带符号的参数
//【:cate_id】表示只能带/的可选参数
Route::get('product-<goods_id>', 'home/goods/index','GET')->pattern(['goods_id' => '\d+']);
Route::get('category-<cate_id>', 'home/search/index','GET')->pattern(['cate_id' => '\d+']);
