Thinkphp
Allen 2022-10-20 00:30:46 3063 0 0 0 0
Thinkphp,Thinkphp笔记,TP6,url访问模式,URL中省略index.php配置方法,URL兼容模式

04,url访问模式

https://www.bilibili.com/video/BV12E411y7u8/

一,URL解析

多应用: http: / / serverName/index.php/应用/控制器/操作/参数/值...;

单应用: http: / / serverName/index.php/控制器/操作/参数/值...;

新建控制器:Test.php

namespace appcontroller;
class Test
{
	public function index()
	{
		return "test-->index ";
	}
	public function hello($name='')
	{
		return "hello ".$name;
	}
}

...

访问地址:

http://localhost/index.php/test/hello/name/world

http://localhost:8000/index.php/test/hello/name/world

...

URL中省略index.php配置方法

public/index.php 中的index.php可以省略,只要设置URL重写即可;

设置URL重写方法:

打开方法:打开httpd.conf,修改以下
1,启用以下行(将前面的#去掉)
LoadModule rewrite_module modules/mod_rewrite.so
2,找到AllowOverride,修改AllowOverride none为AllowOverride all
然后,重启服务。
http://localhost:8000/index.php/test/hello/name/world
就可以用以下地址访问:
http://localhost:8000/test/hello/name/world

...

二,URL兼容模式

1.上个要点已经了解了URL所有访问规则,通过创建Test控制器更加了

2.如果上面那种形式的URL不支持的话,可以使用兼容模式的方式来访问:

http://localhost:8000/?s=test/hello/name/world

这种方式很少用。


PS:

按上面一中的方法修改后,提示错误如下:

The requested URL /test/hello/name/world was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

在网上查到官网给的解决方案: 解决重写URL,省去index.php问题,修改.htaccess文件

tp6官网文档public目录中给的 .htaccess 文件内容如下

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]  #这一行踩坑,将这一行换成下面的
</IfModule>

正确的写法应该如下,官网给的最后一行配置错误

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

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]  #这才是对的,改成这句保存即可
</IfModule>

重启服务即可。

但在tp6根目录中增加了这个文件后,提示如下错误:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

忽然想到这不是tp的应用目录,应该在public目录中,打开一看,果然已经有.htaccess文件了,打开发现文件是空白的,将上述内容写入保存,重启服务后,OK了。


【版權聲明】
本文爲原創,遵循CC 4.0 BY-SA版權協議!轉載時請附上原文鏈接及本聲明。
原文鏈接:https://tdlib.com/am.php?t=Oh8PHrSHZAo2
Tag: Thinkphp Thinkphp笔记
歡迎評論
未登錄,
請先 [ 註冊 ] or [ 登錄 ]
(一分鍾即可完成註冊!)
返回首頁     ·   返回[Thinkphp]   ·   返回頂部