-
Notifications
You must be signed in to change notification settings - Fork 12
Context root is 404 without trailing slash #30
Description
I've discovered that contexts are not properly matched when the URI is the context root and there is no trailing slash. E.g., given a context with the settings base_url set to /context/ and site_url set to https://www.domain.com/context/ the URI /context/ will load but /context is 404. I modified the following code to fix it:
`// find matching hosts
$matched_contexts = $contexts['_hosts'][$http_host];
foreach ((array) $matched_contexts as $index => $ckey) {
$context = $contexts[$ckey];
$cbase = $contexts[$ckey]['base_url'];
$requestUrl = rtrim($requestUrl, '/').'/';
$strpos = strpos($requestUrl, $cbase);
if ($strpos === 0) {
$matches[strlen($contexts[$ckey]['base_url'])] = $ckey;
}
}
// modify request for the matched context
if (!empty($matches)) {
$cSettings = $contexts[$matches[max(array_keys($matches))]];
$cKey = $matches[max(array_keys($matches))];
// do we need to switch the context?
if ($modx->context->get('key') != $cKey) {
$modx->switchContext($cKey);
}
// remove base_url from request query
if ($cSettings['base_url'] != $modx_base_url) {
$newRequestUrl = str_replace($cSettings['base_url'],'',$requestUrl);
if(strpos($newRequestUrl, 'apply/') != -1) $newRequestUrl = rtrim($newRequestUrl, '/');
$_REQUEST[$modx->getOption('request_param_alias', null, 'q')] = $newRequestUrl;
}
} else if ($_REQUEST['xrouting-debug'] != '1' || !$modx->getOption('xrouting.allow_debug_info', null, false)) {
// if no match found
if ($modx->getOption('xrouting.show_no_match_error', null, true)) {
$modx->sendErrorPage();
} else {
$modx->switchContext($modx->getOption('xrouting.default_context', null, 'web'));
}
}`
This works but there's a weird quirk I don't understand. When a Rewrite Rule is matched in htaccess it creates an infinite loop with certain ones (not all of them). Trimming the trailing slash from one of the redirects (you can see where i'm doing this in the "remove base_url" block) somehow fixes the issue for every redirect even though the URLs are totally unrelated.