Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复typecho在二级目录下,使用伪静态url中不包含二级路径情况下的问题 #1757

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ihewro
Copy link

@ihewro ihewro commented Apr 12, 2024

场景:

当typecho安装在子目录,比如 /blog 目录下
普通情况下,需要访问 xxxx.com/blog/yyy 才能访问到typecho资源,即requestUri 和 baseUrl 起始位置是匹配的。

但是可以通过地址重写,当访问 xxxx.com/yyy 的时候,实际访问资源是/blog/yyy 路径,

apache下的.htaccess 举例:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ blog/$1 [QSA]
</IfModule>

存在的问题

typecho目前的rootUrl 返回的仍然是根据 getBaseUrl() 拼接出来的。
getBaseUrl() 是真实资源访问的路径,因此在访问博客后台的时候,会重定向到 xxx.com/blog/admin 路径下,而不能直接通过xxx.com/admin 来访问后台。

当简单的通过config.inc.php 手动定义 __TYPECHO_ROOT_URL__,会存在两个问题:

  1. 访问 xxx.com/admin 的时候 重定向的地址不正确,原因是 ___rootUrl() 实现中,如果手动定义了__TYPECHO_ROOT_URL__ 为xxx.com,也会减去 "/admin"的长度,而__TYPECHO_ROOT_URL__ 本身没有包含"/admin" 字符串,导致返回的rootUrl是残缺的。

  2. 修正第一点问题后,getPathInfo 返回的值不正确:

比如 访问 xxx.com/index.php/action/xxx?ref=xxxx

  • requestUri : /index.php/action/xxx?ref=xxxx
  • baseUrl: /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 配置

<IfModule mod_rewrite.c>
RewriteEngine On

# 不带子目录路径重写到子目录上
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ blog/$1 [QSA]

# 伪静态
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blog/index.php/$1 [L]

</IfModule>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant