Skip to main content
Last Reviewed: August 01, 2022

Redirect with PHP

Learn how to redirect with PHP.

If your site configuration prevents you from setting the primary domain from the platform level, you can use PHP redirects. However, redirecting the platform domain will break the screenshot of your site in the Personal Workspace, and might complicate troubleshooting for our Support team.

AGCDN only works with custom domains. This means that domains are not covered. With AGCDN, a site will not be fully protected under WAF if it is using the platform domain. A platform domain redirect to the main domain is recommended.

Add the following to wp-config.php, usually placed above /* That's all, stop editing! Happy Pressing. */. Don't forget to replace

if (isset($_ENV['PANTHEON_ENVIRONMENT']) && php_sapi_name() != 'cli') {
  // Redirect to https://$primary_domain in the Live environment
  if ($_ENV['PANTHEON_ENVIRONMENT'] === 'live') {
    // Replace with your registered domain name.
    $primary_domain = '';
  else {
    // Redirect to HTTPS on every Pantheon environment.
    $primary_domain = $_SERVER['HTTP_HOST'];

  $requires_redirect = false;
  // Ensure the site is being served from the primary domain.
  if ($_SERVER['HTTP_HOST'] != $primary_domain) {
    $requires_redirect = true;

  // If you're not using HSTS in the pantheon.yml file, uncomment this next block.
  // if (!isset($_SERVER['HTTP_USER_AGENT_HTTPS'])
  //     || $_SERVER['HTTP_USER_AGENT_HTTPS'] != 'ON') {
  //   $requires_redirect = true;
  // }

  if ($requires_redirect === true) {

    // Name transaction "redirect" in New Relic for improved reporting (optional).
    if (extension_loaded('newrelic')) {

    header('HTTP/1.0 301 Moved Permanently');
    header('Location: https://'. $primary_domain . $_SERVER['REQUEST_URI']);

WordPress users should also run a search and replace to update any references to the platform domain.

Convert Multiple .htaccess Redirects and Rewrites to PHP

If you need to convert a large number of .htaccess redirects or rewrites to PHP, feel free to utilize our free script for both WordPress and Drupal.

Redirect Traffic to Your Live Site/Domain from $

There are several ways you can use PHP to redirect traffic to your live site or domain from $

Basic Use Case

wp-config.php or settings.php
 if($_SERVER['HTTP_HOST'] == '') {
    header('HTTP/1.0 301 Moved Permanently');

Using RegEx

wp-config.php or settings.php
if (preg_match('/^live-(.+)\.pantheonsite\.io$/', $_SERVER["HTTP_HOST"], $matches)) {
		$redirect_url = 'https://www.' . $matches[1] . '.com';
    $response_code = 301;

Without Regex

wp-config.php or settings.php
if ($_SERVER["HTTP_HOST"] == "") {
    header('HTTP/1.0 301 Moved Permanently');
    header('Location: https://www.' . $matches[1] . '.com/');

Redirect a /requested/path

wp-config.php or settings.php
if($_SERVER["REQUEST_URI"] == '/old/path') {
  header('HTTP/1.0 301 Moved Permanently');
  header('Location: /new/path/');