<?php
/*第一层意义:
*定义的是与thinkphp有关的核心框架文件目录路径,它可以通过这一个常量在以后运行的时候都去找这个路径,
*确保在以后运行过程中,绝对不会出现问题的(绝对不会对整个项目运行加载路径产生错误);
*第二层意义:
*做一个 *** 作(放跳墙),是防止用直接访问我们的敏感文件,怎么避免呢,我就可以做一个页面包含整个
*敏感页面,用户的访问必须通过页面(A)来访问,在A页面处理好与安全相关的事宜 */
代码:
<?php
define('THINK_PATH', './ThinkPHP/')
define('APP_NAME', '14')
define('APP_PATH', '.')
require(THINK_PATH . "ThinkPHP.php")
App::run()
?>
vue在服务端部署时,我们都知道通过npm run build 指令打包好的dist文件,通过http指定是可以直接浏览的,Thinkphp通过域名指向index.php文件才可以浏览。要使前端正常调用后端数据,有两种方法:1、前端跨域调用后端数据,2、前端打包文件部署在后端的服务器文件夹下(同域)。
web服务器: apache
一、跨域
在服务器配置站点:
在路径/home/www/ 下创建test项目文件夹,用来放项目文件。
找到httpd-vhosts.conf文件配置站点
前端站点:
ServerName test.test.com
DocumentRoot "/home/www/test/dist"
DirectoryIndex index.html
后端站点:
ServerName test.testphp.com
DocumentRoot "/home/www/test/php"
DirectoryIndex index.php
将前端打包好的dist文件放在/home/www/test/ 文件夹下,运行http://test.test.com可浏览,当路径改变时,刷新会出现404错误。此时dist文件下创建一个.htaccess文件,当路径不存在时,路径指向http://test.test.com/index.html能解决此问题。
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
在/home/www/test文件夹下创建项目根目录php文件夹,将thinkphp文件放在php下。TP5的入口文件在public文件下,在这将public下的入口文件index.php挪到php文件夹下(个人习惯将入口文件放在项目根目录), 后端绑定Index模块。
前端调用后端接口,存在跨域,跨域解决方法有好几种,在这我将在后端php做配置,解决跨域问题,在公用控制器设置跨域配置:
class Common extends Controller
{
public $param
// 设置跨域访问
public function _initialize()
{
parent::_initialize()
isset($_SERVER['HTTP_ORIGIN']) ? header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']) : ''
header('Access-Control-Allow-Credentials: true')
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS')
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authKey, sessionId")
$param = Request::instance()->param()
$this->param = $param
}
}
前端调用登录接口: this.axios.post('http://test.testphp.com/index.php/base/login', {user: '', password: ''})。
(可在webpack.base.conf.js文件下可定义接口:http://test.testphp.com/index.php/)
二、同域
后端配置同上,公共配置器中的header配置注释。将前端的dist文件下的所有文件(包含.htaccess),放在php文件夹下。将后端index控制器的index方法的路径重定向php下的index.html文件:
namespace app\index\controller
use think\Controller
class Index extends Controller
{
public function index() {
$this->redirect('/index.html')
}
}
前端调用登录接口: this.axios.post('/index.php/base/login', {user: '', password: ''})
转自:https://blog.csdn.net/qq_35465132/article/details/78986675
最近也遇到了这个问题,顺便回答一下,我这边前后台入口文件都在一个文件夹里,想配置成以下这样:
前后:www.xxx.com/控制器/方法 (模块已配置故不展示)
后台:www.yyy.com/控制器/方法 (模块已配置故不展示)
所以需要准备两个域名,分别访问前后台,然后在.htaccess文件中通过匹配域名的方式来决定进入哪个规则,所以配置成了下面这样:
<IfModule mod_rewrite.c>Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^.*xxx\.com$
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
RewriteCond %{HTTP_HOST} ^.*yyy\.com$
RewriteRule ^(.*)$ /admin.php/$1 [QSA,PT,L]
</IfModule>
结果前台没问题而后台报错,页面展示:
日志文件里显示:Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace
后来搜索资料解决了这个问题,在.htaccess文件中添加如下两行
RewriteCond %{ENV:REDIRECT_STATUS} 200RewriteRule .* - [L]
这两行代码是用来停止重定向无限循环的,至此前后台入口文件都得到了隐藏,希望能帮助后面的人,全部配置代码是:
<IfModule mod_rewrite.c>Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^.*xxx\.com$
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
RewriteCond %{HTTP_HOST} ^.*yyy\.com$
RewriteRule ^(.*)$ /admin.php/$1 [QSA,PT,L]
</IfModule>
这个答案也可以回答重定向次数限制的问题,如果出现这个错误,则考虑是不是重定向无限循环了,至于为什么上面的配置会出现这个错误,我也不明白,同时希望有明白的可以指教。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)