documentation icon indicating copy to clipboard operation
documentation copied to clipboard

"Without regex" example uses regex

Open alisonjo315 opened this issue 2 years ago • 4 comments

As far as I can tell, line 55 expects a regex ($matches[1]): https://github.com/pantheon-systems/documentation/blob/0a2ce819ff3dcee974a3ed24f95c2891592fb06d/source/content/guides/redirect/07-php.md?plain=1#L50-L57

I'm not sure what the proper code is, the only other version I've been able to find is for when you also need to force HTTPS in settings.php, not just the primary domain (i.e. https://docs.pantheon.io/guides/domains/primary-domain#redirect-with-php )

Is it as simple as replacing $matches[1] with mysite ? -- i.e. line 55 would be:

    header('Location: https://www.mysite.com/');

alisonjo315 avatar Aug 08 '23 21:08 alisonjo315

I agree. I don't understand what this section of this page is supposed to do:

https://docs.pantheon.io/guides/redirect/php#redirect-traffic-to-your-live-sitedomain-from-sitepantheonio

As written, these snippets will redirect all requests that match a certain criteria to the home page. These would be more helpful if it kept the path string like the code at the top of this page? Very confusing.

I've never noticed this part of the page before and I don't understand how these got here and what they're for. They don't seem useful.

dficker avatar Aug 08 '23 21:08 dficker

Yeah! -- after I posted this issue, I realized that, too -- I don't know what the value is, of those code snippets. (The "without regex" example also does have a mistake, but yeah also, "who is this for?")

alisonjo315 avatar Aug 08 '23 22:08 alisonjo315

Here's what we have on a site that happens to have newrelic, too (in settings.php) -- a bit more concise than the longer code block with the force HTTPS bit. The newrelic part is obviously "optional" (and not relevant to all sites). but I'm including it for the sake of sharing, because this is literally the code we have working well on a real-live site (minus the fake domain).

if (isset($_SERVER['PANTHEON_ENVIRONMENT']) && php_sapi_name() != 'cli' && $_ENV['PANTHEON_ENVIRONMENT'] === 'live') {
  $primary_domain = 'mysite.com';
  if ($_SERVER['HTTP_HOST'] != $primary_domain) {
    # Name transaction "redirect" in New Relic for improved reporting (optional)
    if (extension_loaded('newrelic')) {
      newrelic_name_transaction("redirect");
    }
    header('HTTP/1.0 301 Moved Permanently');
    header('Location: https://'. $primary_domain . $_SERVER['REQUEST_URI']);
    exit();
  }
}

alisonjo315 avatar Aug 09 '23 00:08 alisonjo315

Yeah the "Without Regex" code contains an error. It does strike me as a bit unnecessary to have a non-regex version but I'm guessing this is for people who are allergic to regex and would really prefer something without the confusing symbols involved.

The "Using RegEx" one assumes the pantheon site name is exactly the same as the domain name, i.e. live-mysite.pantheon.io will redirect to www.mysite.com, not www.mysite.org, not www.myproduct.com, etc.

@alisonjo315 your example is most correct as it won't attempt to redirect in non-pantheon environments (like local development), the new relic section is harmless for sites without new relic, and the php_sapi_name() condition is prevents the redirect code from breaking drush in the case of drupal sites, and omitting it would probably break wp-cli for wordpress sites as well. The live environment check also avoids redirecting from non-live environments which is always (or almost always) desirable as well.

namespacebrian avatar Aug 10 '23 20:08 namespacebrian

I think we can just remove these sections: https://github.com/pantheon-systems/documentation/pull/9086

stevector avatar Jul 10 '24 19:07 stevector