Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Allow developers to create complex domain access rules #2

Closed
straube opened this issue Jul 28, 2016 · 3 comments
Closed

Allow developers to create complex domain access rules #2

straube opened this issue Jul 28, 2016 · 3 comments

Comments

@straube
Copy link
Owner

straube commented Jul 28, 2016

Despite the possibility of setting a base URL for each domain, it may be interesting to allow developers to create complex domain access rules. The behavior should be kept the same as we have today: if the user is accessing a disallowed URL he/she will be redirected to an allowed URL.

A filter is probably the best solution do achieve this:

add_filter('multiple_domain_redirect', function ($url, $domain) {

    // URL verification logic.

    return $url;
}, 10, 2);
@straube straube added this to the 0.3 milestone Jul 28, 2016
@straube straube self-assigned this Jul 28, 2016
straube added a commit that referenced this issue Jul 28, 2016
@straube
Copy link
Owner Author

straube commented Jul 28, 2016

I end up using an action instead a filter. It made more sense.

The default logic uses the base URL from the plugin settings, this way:

if (!empty($this->domains[$this->domain])) {
    $base = $this->domains[$this->domain];
    if (!empty($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], $base) !== 0) {
        wp_redirect(home_url($base));
        exit;
    }
}

A developer may create a custom logic adding a new function to multiple_domain_redirect action. There is only one expected param: the current domain.

Here is an example on how to use the action:

function my_custom_redirect($domain)
{
    // Do nothing if the request is using the original domain.
    if ($domain === MULTPLE_DOMAIN_ORIGINAL_DOMAIN) {
        return;
    }
    // If the URI doesn't start with /cool/path, redirect the user to the original domain.
    if (!empty($_SERVER['REQUEST_URI']) && !preg_match('/^\/cool\/path/i', $_SERVER['REQUEST_URI'])) {
        wp_redirect('http://' . MULTPLE_DOMAIN_ORIGINAL_DOMAIN . '/');
        exit;
    }
}

add_action('multiple_domain_redirect', 'my_custom_redirect');

@straube straube closed this as completed Jul 28, 2016
@straube
Copy link
Owner Author

straube commented Feb 11, 2020

Following the support case in here, I think the plugin needs a way to prevent redirects based on custom logic. Keeping the current action hook, a filter also could be added. In case the filter returns an empty value (null or false), the redirect could be prevented.

@straube
Copy link
Owner Author

straube commented Oct 29, 2020

Besides creating a custom redirect logic, one can also disable the built-in redirect using a new filter called multiple_domain_disable_redirect. Here's an example on how to use it:

function my_custom_redirect_check($domain) {
    if ($domain === "mydomain.com") {
        return true;
    }
}

add_filter('multiple_domain_disable_redirect', 'my_custom_redirect_check');

When the filter returns true, the redirect will be disabled.

straube added a commit that referenced this issue Oct 29, 2020
Added filter to programmatically disable redirects. Fixes #2
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant