0

I'm in predicament where I must use settings.php to do what I'd normally just do via mod_rewrite... the site is being hosted on Pantheon and I am referring to this article: http://helpdesk.getpantheon.com/customer/portal/articles/368354

I need to send the original requested path for reference. Trying to pass a the SERVER_URI as a parameter via header location in setting.php gets me an infinite loop.

My code:

if (preg_match('/article\/[0-9]?[0-9]\/[0-9]?[0-9]\/[aA-zZ].+/', $_SERVER['REQUEST_URI'])) {
    header('HTTP/1.0 301 Moved Permanently');

    header("Location: http://answers3x-ec-core-community.gotpantheon.com/catchall/?dest=" . urlencode($_SERVER["REQUEST_URI"]));  exit;
}

Anybody have any suggestions as to how I might accomplish passing the $_SERVER["REQUEST_URI"] via header location from settings.php without an infinite loop?

  • Why don't you just use Path redirect module? Why does it have to be settings.php? Doing it inside PHP I can understand, forcing it into settings when there is perfectly fine, if a bit undermaintained, module seems weird. – Mołot Aug 21 '14 at 21:56
  • I would use path redirect or similar D7 version, but haven't found one that would work - have custom module intended to catch this 1st redirect to rewrite the path alias using tokens after identifying the old path pattern (which why I need reference REQUEST_URI) and then 2nd redirect to final location. – vrwired Aug 21 '14 at 22:21
  • D7 version was moved to a new project, my bad. But the link is there on D6 page. Here it is, for future reference: drupal.org/project/redirect. As far as I remember, it was pretty workable. – Mołot Aug 22 '14 at 6:44
  • 1
    Change your regex string to start with /^. Or maybe /^\/ depending on your web server setup. That should stop the loop – Clive Aug 22 '14 at 7:22
  • thank you Clive, I at last got chance to test this and it did indeed limit what I needed. – vrwired Aug 22 '14 at 22:11
2

While it's technically possible to use Drupal contrib to perform the redirect, it's faster and more efficient to redirect without having to fully bootstrap your web application.

For web only actions, like redirects, check for the existence of $_SERVER['PANTHEON_ENVIRONMENT'] - if it exists, it will contain a string with the current environment (dev, test or live).

To perform redirection for the pattern you've supplied, make sure to only match for it at the beginning of the URI in order to avoid an infinite loop problem:

// Pantheon - web only.
if (isset($_SERVER['PANTHEON_ENVIRONMENT'])) {
    // Check if you only want redirection from the live web environment.
    if ($_SERVER['PANTHEON_ENVIRONMENT'] == 'live') {
        if (preg_match('/^\/article\/[0-9]?[0-9]\/[0-9]?[0-9]\/[aA-zZ].+/', $_SERVER['REQUEST_URI'])) {
            header('HTTP/1.0 301 Moved Permanently');
            header('Location: http://answers3x-ec-core-community.gotpantheon.com/catchall/?dest=' . $_SERVER["REQUEST_URI"]);
            exit();
        }
    }
}

Sources:

Configuring settings.php

PHP preg_match() Examples

preg_match

  • 1
    thanks for the Pantheon details Suzanne - Clive was right on with my need to delimit by caret – vrwired Aug 22 '14 at 22:18
0

In the end what really I needed to accomplish was a way to grep for a contained (not beginning) directory name in the path that wouldn't bomb into an infinite loop continually calling settings.php. What worked for me was to negate the regex like so:

if(preg_match('/^((?!catchall).)*$/', $_SERVER['REQUEST_URI'])) {

since 'catchall' is the directory I am redirecting to including the old path as a parameter, it allows for the redirect to get to this page (and custom module to handle it) without an infinite loop. I basically surrounded the above check first around all preg_match checks for the particular patterns I needed to find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.