前提:
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
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
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>