Git Product home page Git Product logo

xrouting's Introduction

Deprecated

This extra is no longer maintained. In case you need to do custom context routing we do recommend looking at modxcms/revolution#16045.

XRouting

XRouting is a simple plugin that handles requests for different contexts. It automatically switches the context based on a (sub)domain AND/OR subfolder.

It's like the Gateway plugin from the docs, except you don't have to manually edit the plugin: it takes the http_host and base_url settings you have already configured in your context and routes based on that. It caches the http_host/base_url-to-context relation so it doesn't perform excessive database lookups.

You can also use http_host_aliases to route multiple domains to one context.

Instructions

All you need to do is to install this plugin and make sure your contexts have http_host, base_url, site_url and site_start context settings set.

/!\ Please make sure to add your http_host and site_url without www. if the xrouting.include_www setting is enabled (default!)

System Settings

You can set the following system settings:

  • xrouting.include_www - automatically include www subdomain (default: yes)
  • xrouting.show_no_match_error - set to true to show the error page if no matching context has been found (false will show the default context instead)
  • xrouting.default_context - define the default context if no matching context has been found and xrouting.show_no_match_error is false
  • xrouting.allow_debug_info - activate this setting and add &xrouting-debug=1 to your URL and you will get a handy debug output if your routing isn't working as expected

Troubleshooting

If your context routing isn't working as expected you can active the xrouting.allow_debug_info system setting and add &xrouting-debug=1 to your URL to get a handy debug output. If you can't find any issue in your debug output feel free to open an issue and paste your debug output into the issue.

xrouting's People

Contributors

benjamindean avatar christianseel avatar jako avatar wfoojjaec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xrouting's Issues

[Q] Would there be any issues redirecting to a context with a base_url of just "/" ?

Hope you don't mind me asking this question here...happy to close this ticket if you don't have time to answer:

I have a working setup based on your guide here...
https://modx.today/posts/2015/05/using-xrouting-for-multilingual-websites-in-modx

I was wondering, if I don't want /en/ in the base_url for my english context, would this cause issues with the way Xrouting works?

So rather than domain.com > domain.com/en/ I just get domain.com > domain.com?

I have this working, I just wanted to see if there might be potential issues with this approach?

PHP 8 warnings

Getting these warnings from PHP8. Just thought I'd log them somewhere.

XRouting plugin:

line 100: PHP warning: Undefined array key "q"
line 146: PHP warning: Undefined array key "xrouting-debug"

Plugin not firing

Hi guys,

I've been using XRouting on many installs now without issues, but for the latest one the plugin fails to execute.

The &xrouting-debug=1 setting doesn't work when appended to the URL and activated in system settings, so it's not a matter of the plugin not rerouting properly. It simply doesn't fire. I reinstalled the plugin and checked if the proper system events were enabled, but that seems to be in order (OnContextRemove, OnContextSave, OnHandleRequest, OnSiteRefresh).

I then disabled the plugin and installed ContextRouter instead. This immediately routed to the correct subdomain, so all context and .htaccess settings seem to be correct. The error log is silent (on this one.. ;)).

That's not really much to go on, so just PM me if you'd like to debug it in the manager yourself. I'm good for now with ContextRouter.

Freezing/timeout loop with 2 contexts and subfolders

I'm trying to create 2 context with leading "www" on hostnames, with "include_www" setting turned off.

The contexts are "web" and "intranet", but when I try to access "intranet" I am getting some kind of loop or timeout in MODX (2.5.7), which causes the intranet never load.

When using hte debug feature, the debug information shows correct routes and everything is perfectly fast, but when outside of debug mode, the site freezes and the intranet never loads. See the attached screenshot of all context/settings:

issue

Any idea here, what might be wrong?

xrouting_contextmap.cache.php contains contexts limited to the user who triggered the refresh

Using xrouting to host 11 contexts on a single install. Each context is a subdomain of the main domain.

XRouting seems to have been working fine for several months, however recently we've been having issues where some contexts will not be accessible and will return the error page for the default context. When this happens the main context will still be listed in the cache file, along with one of the other 10 contexts.

We believe this could be down to us defining that users within the mgr context only have access to two contexts: the main context and the context they have context permissions to access.

To temporarily fix this we've commented out the 'OnSiteRefresh' event within the plugin, but not sure if there's something that needs changing in the plugin, or if context access permissions need to be set differently for the plugin to work?

README.MD error about context settings

setting site_url (which will be the complete URI of the site root) doesn't need to be defined without the www, when adding the www-redirect in the system settings. Actually, it must not, and here's why:

Consider a site that has a FormIt Ajax-Call to a responder-template that responds JSON. This will have the site_url domain.com and be receiving a request from www.domain.com and therefore violate the CORS policy unless you explicitly allow cross-origin requests (which will be too high-level for most users).

Correct config would consequently be site_url WITH the www - so the final version of it and http_host WITHOUT it to make it redirectable.

Both domains showing 404 errors

I built my site using ModX and it's been working flawlessly. I decided to build a new site and manage it with the same installation of ModX. I'm using [b]Xrouting[/b] for this.

I went ahead and pointed my domain2 (laredomusicawards.com) to the ModX install directory... Checked that it was pointing to domain1 (sldgtv.com) and it was.

I then installed Xrouting.

Then I created my new context with the following settings:

Now both domains show a 404 error.

Then I tried this same thing on another ModX install and it worked. The only difference with this install is I didn't have Friendly URLs on (yet) and I hadn't renamed the ht.acces file.

Any ideas what it could be? What am I doing wrong?

$modx_base_url uses web context base_url

If the web context is used with a base_url setting that differs from / it beaks XRouting, because $modx->getOption('base_url', null, MODX_BASE_URL); uses the context settings from the already initialized context (web, in index.php).

Use something like

$baseUrlSetting = $modx->getObject('modSystemSetting', ‚base_url');
$modx_base_url = $baseUrlSetting->get('value');
if (!$modx_base_url) $modx_base_url = MODX_BASE_URL;

or just MODX_BASE_URL instead!

[Q] How to redirect www to non-www host?

After installing the plugin, if I leave this code in .htaccess:

# Rewrite www.domain.com -> domain.com -- used with SEO Strict URLs plugin
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]

All requests gets redirected to default web context. If I comment out the above lines, it works perfectly, but serves separately to www and non-www.

Kindly advise.

XRouting - Loses secondary Context, requires Clear Cache to start working again

Hello,

I had enabled the xrouting.allow_debug_info in my System Settings. When /gb/service.html Resource returned a HTTP 404, I added the &xrouting-debug=1 to the URL. It shows the secondary Context was not loading.

I ran Manage -> Clear Cache. The /gb/service.html Resource was returned. When I added the &xrouting-debug=1 to the URL, it shows both Contexts.

MarkH had suggested adding my Groups to the Context Access Permissions. The Groups were added with 'Authority' = 9999, 'Access Policy' = Resource

https://community.modx.com/t/xrouting-contexts-secondary-context-errors-with-404/5188

Do you have any suggestions as to what I could try?

Thank you!

######################################################################################

BAD Session - Returns 404

######################################################################################

MODX context map:

Array
(
[_hosts] => Array
(
[apps.cipcontractors.com] => Array
(
[0] => web
)
)

[web] => Array
    (
        [assets_url] => /pe/assets/
        [base_url] => /pe/
        [error_page] => 1
        [http_host] => apps.cipcontractors.com
        [site_name] => Prairie Electric
        [site_start] => 1
        [site_url] => https://apps.cipcontractors.com/pe/
        [unauthorized_page] => 1
    )

)

Requested URL: service.html

Requested URL with base_url: /gb/service.html

Matched context(s) (Array key defines match quality):

Array
(
)

Request will go to context:

Modified request URL:

######################################################################################

GOOD Session - Returns Resource

######################################################################################

MODX context map:

Array
(
[_hosts] => Array
(
[apps.cipcontractors.com] => Array
(
[0] => web
[1] => gb
)
)

[web] => Array
    (
        [assets_url] => /pe/assets/
        [base_url] => /pe/
        [error_page] => 1
        [http_host] => apps.cipcontractors.com
        [site_name] => Prairie Electric
        [site_start] => 1
        [site_url] => https://apps.cipcontractors.com/pe/
        [unauthorized_page] => 1
    )

[gb] => Array
    (
        [assets_url] => /gb/assets/
        [base_url] => /gb/
        [error_page] => 157
        [http_host] => apps.cipcontractors.com
        [site_name] => GB Manchester
        [site_start] => 157
        [site_url] => https://apps.cipcontractors.com/gb/
        [unauthorized_page] => 157
    )

)

Requested URL: service.html

Requested URL with base_url: /gb/service.html

Matched context(s) (Array key defines match quality):

Array
(
[4] => gb
)

Request will go to context: gb

Modified request URL:

Routing not working

I cannot get this plugin to work, bu I'm probably missing something.

I've set up my contexts as

web:
    http_host: example.com
    base_url: /
ajax:
    http_host: example.com
    base_url: /ajax/

I've tried debugging it and it correctly switches to the ajax context, but it then runs a second request, which instead returns the web context. It cannot find a match against ajax anymore because the request url has changed.

I tried commenting out the request url changes after the context switch, which stopped it from running a second request matching, but it still doesn't work. I always get sent to the error page.

I have also set the appropriate site_start settings on both contexts. Didn't make a difference however.

Any ideas?

Server slow after database deployment

After deploying database from developer server to production I am experiencing 15-20 minutes of downtime (really slow loading). The issue appeared after installing and configuring XRounting. Probably caching issue?

check if build script is working

I'm not sure if the build script is working. This needs some work I guess and system settings are currently not packaged I guess.

If someone else has a ready to use build script, feel free to submit a PR.

Subfolder direction not working

For some reason I couldn't get the subfolder feature working. We had used the Gateway plugin before, but decided to switch to XRouting now that this better option is available. I had set up three contexts out of which two are on one domain with different base_urls and the third on it's own domain.

web:
    base_url: /
    cultureKey: fi
    http_host: www.exampledomain.fi
    site_url: http://www.exampledomain.fi/

swe:
    base_url: /sv/
    cultureKey: sv
    http_host: www.exampledomain.fi
    site_url: http://www.exampledomain.fi/sv/

se:
    base_url: /
    cultureKey: sv
    http_host: www.exampledomain.se
    site_url: http://www.exampledomain.se

With the above setup the two domains with root base_url worked perfectly, but the subfolder was ignored and returned the web context instead. Maybe I missed something?

We resorted to putting the swe context on a subdomain instead, which works, but since this is a feature I assume this is either a bug or fail on my part.

Wrong image paths created by pthumb

Hi Chris,

In my current project I am trying out Xrouting for a multilanguage site. When I use pthumb to create images, it is adding the base_url '/de/' to the path name of the image. But actually the 'de' folder does not exist on the server.
How do you take care of this problem?

Cheers,
Ben

Installation in subdirectory?

Does this work in an installation in a subdirectory? For example, if I have MODX installed in /htdocs/revo1/ how would I need to set the http_host and base_url? Do I need to set these for the 'web' context as well?

context routing not working as expected

I have two contexts needed to be routed.

flestic.com -> route to context 'flestic.nl'
flestic.com/en/ -> route to context 'flestic-en'

Debug info returns correct info (routing to correct context), but the routing doesn't take place.
Al requests are routed to flestic.com/en/

My context settings:
schermafbeelding 2015-09-24 om 17 11 23
schermafbeelding 2015-09-24 om 17 11 33

My Xrouting debug info for correct routing:

## MODX context map:

Array
(
    [_hosts] => Array
        (


            [flestic.com] => Array
                (
                    [0] => flestic.nl
                    [1] => flestic-en
                )

            [flestic.nl] => Array
                (
                    [0] => flestic.nl
                )

            [fme.net] => Array
                (
                    [0] => flestic.nl
                )

            [flestic.eu] => Array
                (
                    [0] => flestic.nl
                )

        )



    [flestic.nl] => Array
        (
            [base_url] => /
            [cultureKey] => nl
            [error_page] => 733
            [http_host] => flestic.com
            [http_host_aliases] => flestic.nl,fme.net,flestic.eu
            [site_start] => 643
            [site_url] => http://www.flestic.com/
            [use_alias_path] => 1
        )

    [flestic-en] => Array
        (
            [base_url] => /en/
            [cultureKey] => en
            [error_page] => 859
            [http_host] => flestic.com
            [http_host_aliases] => 
            [site_start] => 819
            [site_url] => http://www.flestic.com/en/
            [use_alias_path] => 1
        )



)



## Requested URL: werkwijze/


## Requested URL with base_url: /en/werkwijze/


## Matched context(s) (Array key defines match quality):

Array
(
    [1] => flestic.nl
    [4] => flestic-en
)



## Request will go to context: flestic-en


## Modified request URL: werkwijze/

My Xrouting debug info for incorrect routing.
Request will go to context: flestic.nl ==> this doesn't happen, it routs to flestic-en

## MODX context map:

Array
(
    [_hosts] => Array
        (


            [flestic.com] => Array
                (
                    [0] => flestic.nl
                    [1] => flestic-en
                )

            [flestic.nl] => Array
                (
                    [0] => flestic.nl
                )

            [fme.net] => Array
                (
                    [0] => flestic.nl
                )

            [flestic.eu] => Array
                (
                    [0] => flestic.nl
                )

        )



    [flestic.nl] => Array
        (
            [base_url] => /
            [cultureKey] => nl
            [error_page] => 733
            [http_host] => flestic.com
            [http_host_aliases] => flestic.nl,fme.net,flestic.eu
            [site_start] => 643
            [site_url] => http://www.flestic.com/
            [use_alias_path] => 1
        )

    [flestic-en] => Array
        (
            [base_url] => /en/
            [cultureKey] => en
            [error_page] => 859
            [http_host] => flestic.com
            [http_host_aliases] => 
            [site_start] => 819
            [site_url] => http://www.flestic.com/en/
            [use_alias_path] => 1
        )



)



## Requested URL: werkwijze/


## Requested URL with base_url: /werkwijze/


## Matched context(s) (Array key defines match quality):

Array
(
    [1] => flestic.nl
)



## Request will go to context: flestic.nl


## Modified request URL: 

Context root is 404 without trailing slash

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.

undefined index errors coming from xrouting

I'm seeing these errors in my log anytime you navigate to or refresh any page on the site. I'm currently not using friendly urls - so i'm not sure if that is having an impact or what causes these to show up - any ideas on why php is throwing these warnings?


which seems to be coming from this line: 
$requestUrl = str_replace('//','/',$modx_base_url.$_REQUEST[$modx->getOption('request_param_alias', null, 'q')]);

[2021-04-21 23:39:41] (WARN @ /paas/c1038/www/core/cache/includes/elements/modplugin/41.include.cache.php : 146) PHP notice: Undefined index: xrouting-debug

which seems to be coming from this line: 
 if ($_REQUEST['xrouting-debug'] == '1' && $modx->getOption('xrouting.allow_debug_info', null, false)) {

Multiple Domains

Could you add http_host_aliases for routing multiple domains to one context?

note contexts found

In my installation XRouting doesn't found the contexts.
this instaction:
$modx_base_url = $modx->getOption('base_url', null, MODX_BASE_URL);
$requestUrl = str_replace('//','/',$modx_base_url.$_REQUEST[$modx->getOption('request_param_alias', null, 'q')]);

$requestUrl is '/'

foreach ((array) $matched_contexts as $index => $ckey) {

            $context = $contexts[$ckey];
            $strpos = strpos($requestUrl, $contexts[$ckey]['base_url']);

            if ($strpos === 0) {
                $matches[strlen($contexts[$ckey]['base_url'])] = $ckey;
            }

}

In this cycle don't maches beacause $contexts[$ckey]['base_url'] is '/en/'

But i have configure the context in the right way.

MODX Cloud dev accounts?

Can this be used on MODX Cloud accounts where the subdomain URL would be something like "sub.blah.sottwell.modxcloud.com"? I've tried it and am just getting the 'not found' page, but I'm not sure what the settings should be in this case, or if it will even work.

Add option to ignore certain pages in the default context from routing

Even when having a multi-language website with several context's, some pages maybe only exist once within the default context:

  • UpgradeModx (from the UpgradeModx package)
  • Sitemap

As the XRouting plugin tries to redirect every page, calling these pages leads to a 404 / redirect to default context depending on the settings.

It would be nice, if there would be a system setting to add path's / pages which should be ignored from routing.

I enhanced the XRouting plugin like this:

case 'OnHandleRequest':
	if ($modx->context->get('key') == 'mgr') return;
	
	$requestPath = $_REQUEST[$modx->getOption('request_param_alias', null, 'q')];
	if ($requestPath == 'upgrade-modx.html') return;

This enhancement seems to work. Would you accept a PR?

PHP notice: Undefined index: xrouting-debug

After install xrouting my site is very slow on log i recive this:

[2017-09-27 19:00:05] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modplugin/18.include.cache.php : 146) PHP notice: Undefined index: xrouting-debug
[2017-09-27 19:00:08] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modsnippet/21.include.cache.php : 114) PHP notice: Undefined offset: 1
[2017-09-27 19:00:08] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modsnippet/21.include.cache.php : 114) PHP notice: Undefined offset: 1
[2017-09-27 19:00:09] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modsnippet/21.include.cache.php : 114) PHP notice: Undefined offset: 1
[2017-09-27 19:00:09] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modsnippet/21.include.cache.php : 114) PHP notice: Undefined offset: 1
[2017-09-27 19:00:10] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modsnippet/21.include.cache.php : 114) PHP notice: Undefined offset: 1
[2017-09-27 19:00:10] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modsnippet/21.include.cache.php : 114) PHP notice: Undefined offset: 1
[2017-09-27 19:00:10] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modsnippet/21.include.cache.php : 114) PHP notice: Undefined offset: 1
[2017-09-27 19:00:10] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modsnippet/21.include.cache.php : 114) PHP notice: Undefined offset: 1
[2017-09-27 19:00:11] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modsnippet/21.include.cache.php : 114) PHP notice: Undefined offset: 1
[2017-09-27 19:00:11] (WARN @ /var/www/stage.mysite.it/core/cache/includes/elements/modsnippet/21.include.cache.php : 114) PHP notice: Undefined offset: 1

Symlinks Across Different Contexts Result in 503

Fantastic Extra.

Only issue I'm having is if I create a symlink on a second context that points to a resource on the first context, viewing the symlink gives a 503 error.

Symlinks within the same contexts still work fine.

Subfolder end slash

Plugin only works when web address is written with slash at the end "www.domain.com/lang/"
"www.domain.com/lang" results in Page Not Found. The problem is that many people wouldn't care to type slash at the end - they would just hit enter after "lang". Is there any way to redirect www.domain.com/lang to www.domain.com/lang/? Thank You!

PHP warning: Undefined array key "xrouting-debug"

The problem is that the code is trying to access the array key xrouting-debug in the $_REQUEST array, but this key does not exist. This leads to an ‘Undefined array key’ error.

To correct this error, you should check whether the key exists before accessing it. You can do this with the isset or array_key_exists function. Here is a possible solution for your code:

Line 146:
if (isset($_REQUEST['xrouting-debug']) && $_REQUEST['xrouting-debug'] == '1' && $modx->getOption('xrouting.allow_debug_info', null, false)) {
Tested on Modx 2.8.7, PHP 8.1.x, System setting xrouting.allow_debug_info = No;

Best regards,
Stefan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.