Configuring MODx Revolution to work with both http and https

末鹿安然 提交于 2019-12-06 15:00:19

The problem is with $modx->makeUrl(). For example, for the

[[!Login? &redirectToOnFailedAuth=`[[++unauthorized_page]]`]]

call, in core/components/login/controllers/web/Login.php:

public function checkForRedirectOnFailedAuth(modProcessorResponse $response) {
    $redirectToOnFailedAuth = $this->getProperty('redirectToOnFailedAuth',false,'isset');
    if ($redirectToOnFailedAuth && $redirectToOnFailedAuth != $this->modx->resource->get('id')) {
        $p = array(
            'u' => $this->dictionary->get('username'),
        );
        $message = $response->getMessage();
        if (!empty($message)) $params['m'] = $message;
        $url = $this->modx->makeUrl($redirectToOnFailedAuth,'',$p,'full');
        $this->modx->sendRedirect($url);
    }
}

the last two lines do a redirect to a URL generated with makeUrl, which will be something like [[++url_scheme]]www.example.com/etc (note: I'm not 100% sure here, as I can't easily look at the raw URL. The conclusions still hold, though). If the URL is simply shown on the page, this is no problem, because MODx will parse the tag before inserting it into the html output. However, as the URL is used directly for the redirect, no such replacement takes place, and the browser interprets it as a relative URL, resulting in target URLs such as https://www.example.com/[[++url_scheme]]www.example.com/etc.

So much for the problem. To avoid this, site_url must be a literal value without any tags in it. As a workaround, I now use the following snippet as the first thing in my template:

$modx->config['site_url'] = $modx->config['url_scheme'] . substr($modx->config['site_url'], strlen('[[++url_scheme]]'));
return '';

together with a [[++site_url]] of

[[++url_scheme]]www.example.com/

Note that some parts of MODx don't seem to notice this update, which is why it's important to still use [[++url_scheme]] in your site_url. As far as I can tell right now, the parts that don't see the update, stuff like [[~id]], work properly with url_scheme.

EDIT this does of course only fix the "View" buttons in the manager if you tweak the manager templates accordingly.

WARNING this is of course very hacky, and not yet tested very well. The fact that some features do not see the overwritten value means that you're introducing an inconsistency into your website, which may lead to subtle errors! If a more clean solution comes up, go for it!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!