Apache部署thinkphp8项目——隐藏public/index.php之关于路由的踩坑总结整理

前提:

Apache根目录htdocs:8081

thinkphp项目:test

thinkphp路由:api/sys/getinfo(做了路由转发)

thinkphp唯一入口文件:test/public/index.php

正常路由:http://127.0.0.1:8081/test/public/index.php/api/sys/getinfo

需求:隐藏public/index.php

方法1

前端访问:http://127.0.0.1:8081/test/api/sys/getinfo

thinkphp项目根目录:新增.htaccess文件  -  定向到public目录

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine on
  RewriteBase /test
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f

  RewriteCond %{REQUEST_URI} !^/public/
  RewriteRule ^(.*)$ public/$1 [QSA,PT,L]
</IfModule>

test/public目录下:新增.htaccess文件  -  排除图片的访问,其他接口都定向到index.php

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine on
  RewriteBase /test/public
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f

  RewriteCond %{REQUEST_URI} !^/storage/
  RewriteCond %{REQUEST_URI} !\.(ico|png|jpg|txt)$
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
方法2

前端访问:http://127.0.0.1:8090/api/sys/getinfo

配置Apache:httpd.conf

#写自己的Apache路径
Define SRVROOT "D:/Apache24" 

ServerRoot "${SRVROOT}"

Listen 8090
<VirtualHost *:8090>
    ServerName www.test.com
   #定义8090端口的根目录
    DocumentRoot "${SRVROOT}/htdocs/test/public"
    DirectoryIndex index.php

    <Directory "${SRVROOT}/htdocs/test/public">
        Options -Indexes +FollowSymlinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

PHPGoview/public目录新增:.htaccess

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine on

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f

  RewriteCond %{REQUEST_URI} !^/storage/
  RewriteCond %{REQUEST_URI} !\.(ico|png|jpg|txt)$
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DytLisa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值