修复typecho在二级目录下,使用伪静态url中不包含二级路径情况下的问题 #1757
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
场景:
当typecho安装在子目录,比如 /blog 目录下
普通情况下,需要访问 xxxx.com/blog/yyy 才能访问到typecho资源,即requestUri 和 baseUrl 起始位置是匹配的。
但是可以通过地址重写,当访问 xxxx.com/yyy 的时候,实际访问资源是/blog/yyy 路径,
存在的问题:
typecho目前的rootUrl 返回的仍然是根据 getBaseUrl() 拼接出来的。
getBaseUrl() 是真实资源访问的路径,因此在访问博客后台的时候,会重定向到
xxx.com/blog/admin
路径下,而不能直接通过xxx.com/admin 来访问后台。当简单的通过config.inc.php 手动定义
__TYPECHO_ROOT_URL__
,会存在两个问题:访问 xxx.com/admin 的时候 重定向的地址不正确,原因是
___rootUrl
() 实现中,如果手动定义了__TYPECHO_ROOT_URL__
为xxx.com,也会减去 "/admin"的长度,而__TYPECHO_ROOT_URL__
本身没有包含"/admin" 字符串,导致返回的rootUrl是残缺的。修正第一点问题后,getPathInfo 返回的值不正确:
比如 访问
xxx.com/index.php/action/xxx?ref=xxxx
,/index.php/action/xxx?ref=xxxx
/blog/index.php
预期返回的 pathinfo 是
/action/xxx
,原本的实现是将requesturi (不包含?部分)剪掉 baseurl的部分,但是因为这个场景下,两者起始位置不匹配,导致返回的 pathinfo 也是残缺的,比如n/xxx
修复方案:
综上所述,核心是第二个问题,即修改baseurl 的实现,它的返回值应该考虑到请求url和实际访问的资源起始路径有差异的情况
修改第二个问题之后,也无需手动定义 TYPECHO_ROOT_URL ,因为此时返回的rootUrl 已经是和requestUri 关联的地址了,就不会出现重定向到带有 /blog 路径的地址上
第一个问题的修复方案是判断 $rootUrl 是否包含$adminDir,才去减掉对应的长度,修复该问题后,可以在配置文件中定义
__TYPECHO_ROOT_URL__
,这样可以让大部分场景下都能重定向到 xxx.com/yyy 地址上附 apache 配置: