带有域名的绝对URL,而不仅仅是从根目录的绝对URL。

huangapple go评论49阅读模式
英文:

absolute URLs with domain instead of just absolute from root dir

问题

我们有一个网站,其中包含多个子站点(具有不同的子域),这些子站点是外部系统,它们请求主站点的页眉和页脚,并将其放入其自己的页面中。由于子站点的域不同,因此页眉/页脚的所有引用都需要包括主站点的域。

这是通过 config.absRefPrefix = https://www.maindomain.tld/ 来实现的。

由于我们刚刚将该站点从 TYPO3 9 升级到 TYPO3 11(不再使用 realurl),我们注意到:
主域不再出现在菜单链接的生成URL中。所有链接都只是以根目录 / 开头的链接 - 这些链接在子站点上不存在。

这些链接是使用视图帮助器(VH) f:link.typolink 生成的,该视图帮助器从一个具有 renderType = inputLink 的字段中获取页面ID。

为什么 VH 忽略了配置 config.absRefPrefix?(图像包含域!)

如何更改才能在生成的链接中包含域?


我们目前的解决方案是将相对链接替换为绝对链接。但我对此不太满意。

英文:

We have a website where multiple subsites (with different (sub-)domains) belong. These subsites are external systems which request the header and footer of the main site and put it into their sides. As the domain for the subsites is different all references from the header/ footer needs to include the domain of the main site.

This is done with config.absRefPrefix = https://www.maindomain.tld/

As we just updated that site from TYPO3 9 to TYPO3 11 (no longer realurl) we noticed:
the main-domain no longer is in the generated URL of the menu links. All links are just links starting with the root dir / - which do not exist on the subsites.

The links are generated with the VH f:link.typolink from a field with renderType = inputLink which just holds a page-id.

Why is the configuration config.absRefPrefix ignored by the VH? (images does contain the domain!)

What needs to be changed to get the domain in the generated links?


Our current solution is a text replace of the relative links to absolute links. But I'm not happy with it.

答案1

得分: 0

absRefPrefix自TYPO3 9中的siteconfig实施以来已经被弃用。

请参见:https://forge.typo3.org/issues/87919

TYPO3 >= 12
您可以在TypoScript设置中进行以下设置:

config.forceAbsoluteUrls = true

TYPO3 < 12
您可以在TypoScript设置中进行以下设置:

typolink.forceAbsoluteUrl = true

如果这不符合您的要求,您还可以实施一个中间件。我不得不调整上述错误报告中的中间件以使其工作。为此,请执行以下操作:

在您的sitepackage的Configuration/RequestMiddlewares.php中注册中间件:

<?php
return [
    'frontend' => [
        'sitepackage-abs-ref-prefix-' => [
            'target' => VENDOR\ExtName\Middleware\AbsRefPrefixEnforcer::class,
            'before' => [
                'typo3/cms-redirects/redirecthandler'
            ],
            'after' => [
                'typo3/cms-frontend/site',
            ]
        ]
    ]
];

然后实施中间件Classes/Middleware/AbsRefPrefixEnforcer.php:

<?php
namespace VENDOR\ExtName\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\NullResponse;
use TYPO3\CMS\Core\Http\Stream;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
 * 用$GLOBALS['TSFE']->absRefPrefix前缀所有链接和src
 *
 * Class GenerateAbsoluteLinksMiddleware
 * @package Clickstorm\CsNewsletterExport\Middleware
 */
class AbsRefPrefixEnforcer implements MiddlewareInterface
{

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {

        $response = $handler->handle($request);
        if (
            !($response instanceof NullResponse)
            && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
            && $GLOBALS['TSFE']->absRefPrefix
            && $GLOBALS['TSFE']->absRefPrefix !== '/'
            && $GLOBALS['TSFE']->absRefPrefix !== 'auto'
        ) {
            $body = $response->getBody();
            $body->rewind();
            $contents = $response->getBody()->getContents();
            $content = $this->parseRelativeToAbsoluteUrls($contents);
            $body = new Stream('php://temp', 'rw');
            $body->write($content);
            $response = $response->withBody($body);
        }

        return $response;
    }

    protected function parseRelativeToAbsoluteUrls(string $input = ''): string
    {
        // https://thydzik.com/convert-relative-links-to-absolute-links-with-php-regex/
        $prefix = $GLOBALS['TSFE']->absRefPrefix;
        return preg_replace('/((?:href|src) *= *["\'])(\/)/i', "$1$prefix", $input);
    }
}
英文:

The absRefPrefix has somehow been deprecated since the implementation of the siteconfig in TYPO3 9.

See: https://forge.typo3.org/issues/87919

TYPO3 >= 12
You can set following in your TypoScript setup:

config.forceAbsoluteUrls = true

TYPO3 < 12
You can set following in your TypoScript setup:

typolink.forceAbsoluteUrl = true

If that does not fits your requirements, then you could also implement a middleware. I had to adjust the middleware in the above bugreport to get it working. To do so:

Register the middleware in your sitepackage Configuration/RequestMiddlewares.php:

&lt;?php
return [
    &#39;frontend&#39; =&gt; [
        &#39;sitepackage-abs-ref-prefix-&#39; =&gt; [
            &#39;target&#39; =&gt; VENDOR\ExtName\Middleware\AbsRefPrefixEnforcer::class,
            &#39;before&#39; =&gt; [
                &#39;typo3/cms-redirects/redirecthandler&#39;
            ],
            &#39;after&#39; =&gt; [
                &#39;typo3/cms-frontend/site&#39;,
            ]
        ]
    ]
];

And then implement the middleware Classes/Middleware/AbsRefPrefixEnforcer.php:

&lt;?php
namespace VENDOR\ExtName\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\NullResponse;
use TYPO3\CMS\Core\Http\Stream;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
 * prefix all links and src with $GLOBALS[&#39;TSFE&#39;]-&gt;absRefPrefix
 *
 * Class GenerateAbsoluteLinksMiddleware
 * @package Clickstorm\CsNewsletterExport\Middleware
 */
class AbsRefPrefixEnforcer implements MiddlewareInterface
{

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {

        $response = $handler-&gt;handle($request);
        if (
            !($response instanceof NullResponse)
            &amp;&amp; $GLOBALS[&#39;TSFE&#39;] instanceof TypoScriptFrontendController
            &amp;&amp; $GLOBALS[&#39;TSFE&#39;]-&gt;absRefPrefix
            &amp;&amp; $GLOBALS[&#39;TSFE&#39;]-&gt;absRefPrefix !== &#39;/&#39;
            &amp;&amp; $GLOBALS[&#39;TSFE&#39;]-&gt;absRefPrefix !== &#39;auto&#39;
        ) {
            $body = $response-&gt;getBody();
            $body-&gt;rewind();
            $contents = $response-&gt;getBody()-&gt;getContents();
            $content = $this-&gt;parseRelativeToAbsoluteUrls($contents);
            $body = new Stream(&#39;php://temp&#39;, &#39;rw&#39;);
            $body-&gt;write($content);
            $response = $response-&gt;withBody($body);
        }

        return $response;
    }

    protected function parseRelativeToAbsoluteUrls(string $input = &#39;&#39;): string
    {
        // https://thydzik.com/convert-relative-links-to-absolute-links-with-php-regex/
        $prefix = $GLOBALS[&#39;TSFE&#39;]-&gt;absRefPrefix;
        return preg_replace(&#39;/((?:href|src) *= *[\&#39;&quot;])(\/)/i&#39;, &quot;$1$prefix&quot;, $input);
    }
}

huangapple
  • 本文由 发表于 2023年6月19日 22:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定