TP5项目public绑定顶级域名(虚拟主机当中)、域名url二级目录绑定tp5解决方案

由于实际工作需求(其实就是因为公司服务偏java)
要求项目必须以ip/目录的方式开展。(解决方法看2即可
而thinkphp5默认要求搭建在顶级目录下。这就坑了。
于是用了3天找出解决方案。
网上也有不少好的参考案例,再次记下。


1、将虚拟主机当中public目录绑定在顶级域名下

参考apache改.htaccess文件解决。

参考 阿里云虚拟主机部署TP5项目绑定二级目录解决办法
.htaccess改为:

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteBase /
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1?Rewrite [L,QSA]
</IfModule>

如果需要指定域名则增加

RewriteCond %{HTTP_HOST} ^www\.xxx\.com$ [NC]

这里记得public这个关键字,RewriteRule ^(.*)$ public/$1?Rewrite [L,QSA]
这一部分tp5不一样,注意区分。

域名二级目录绑定tp5的public(apache)

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteBase /域名的二级目录/
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1?Rewrite [L,QSA]
</IfModule>

2、域名二级目录绑定tp5解决方案

上nginx

首先确定域名二级目录名称:10.X.X.X/aaa/

直接上nginx.conf部分内容

server {
	    listen       8081;
	    server_name  10.X.X.X;
	    root D:/phpstudy_pro/WWW/aaa/public;
	    index  index.html index.htm index.php;
	    location ~ \.php(.*)$ {
	                    fastcgi_pass   127.0.0.1:9000;
	                    fastcgi_index  index.php;
	                    #下面两句是给fastcgi权限,可以支持 ?s=/module/controller/action的url访问模式
	                    fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
	                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	                    #下面两句才能真正支持 index.php/index/index/index的pathinfo模式
	                    fastcgi_param  PATH_INFO  $fastcgi_path_info;
	                    fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
	                    include        fastcgi_params;
	            }
		location /aaa/ {
				            index  index.html index.htm index.php;
				            #autoindex  on;
				          if (!-e $request_filename) {
				            rewrite  ^/aaa/(.*)$  /aaa/index.php?s=/$1  last;
				            break;
				          }
				        }

		location ~ .*\.(css|js|gif|jpg|jpeg|png|bmp|swf)$ {
             root         D:/phpstudy_pro/WWW/aaa/public;
             expires      30d;
         }

		add_header Content-Security-Policy "default-src 'self' 'unsafe-eval' 'unsafe-inline' fonts.gstatic.com data:;img-src  'self' 'unsafe-eval' 'unsafe-inline' data: blob: ;style-src * 'self' 'unsafe-eval' 'unsafe-inline' fonts.gstatic.com data:";
		add_header X-Content-Type-Options: nosniff;
		add_header X-XSS-Protection "1; mode=block";
		#add_header Access-Control-Allow-Origin: http://www.one.site.com;
		#add_header X-Frame-Options: deny;
		#add_header Strict-Transport-Security: max-age=3600; includeSubDomains;
	}

注意修改部分:
listen 8081;//端口,默认应该是80
server_name 10.X.X.X;//这里应该是域名,或者ip
root D:/phpstudy_pro/WWW/aaa/public;//这里就是项目名,正常就该这么写

rewrite ^/aaa/(.*)$ /aaa/index.php?s=/$1 last;//修改重写路径,这里第一个aaa对应的是url路径。第二个aaa对应的是你public的路径,说实话,第二个aaa可以不用。

root D:/phpstudy_pro/WWW/aaa/public;//这句话意思是相关扩展名的文件的路径,这个aaa应该是上一行的第二个aaa

然后,我将public当中所有的目录都改到了public/aaa下,同时修改index.php和router.php
index.php

<?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 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// [ 应用入口文件 ]

// 定义应用目录
define('APP_PATH', __DIR__ . '/../../application/');
// 加载框架引导文件
require __DIR__ . '/../../thinkphp/start.php';

router.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

if (is_file($_SERVER["DOCUMENT_ROOT"] . $_SERVER["SCRIPT_NAME"])) {
    return false;
} else {
    if (!isset($_SERVER['PATH_INFO'])) {
        $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
    }
    require __DIR__ . "../index.php";
}

ps:由于个人对nginx不是很了解,所以硬扛上线的。
总之:就是改nginx的rewrite、在public下新建前端目录,并改public下的php增加…/
另外,我会抽空测试不改public,只改nginx的方法。
求大神评论。
(问我为啥不用apache?因为phpstudy8.1的apache不认header头,.htaccess不认识header,老报错【其实可能是我不懂具体格式】,所以换了nginx,简单,粗暴~)
记录时间:20200228

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值