Understanding Pantheon Server Level Redirects: A Comprehensive Guide

Pantheon offers a robust platform for website hosting, but it handles server-level redirects differently than traditional Apache servers. This guide explains how Pantheon manages redirects, why .htaccess is not supported, and the recommended methods for implementing redirects on your Pantheon-hosted sites.

Pantheon sites run on nginx, a high-performance web server, instead of Apache. For efficiency and reduced resource consumption, nginx on Pantheon is configured to ignore .htaccess files. This is a standard configuration across all Pantheon sites, and direct modifications to the nginx.conf file are not permitted.

While .htaccess might be a familiar method for redirects on Apache servers, it’s generally not recommended, even in those environments. Pantheon advocates for more efficient and maintainable approaches to manage redirects.

Why Pantheon Recommends Primary Domain and PHP Redirects

Instead of relying on .htaccess, Pantheon recommends two primary methods for handling server-level redirects:

1. Setting a Primary Domain via the Dashboard:

For domain-level redirects, Pantheon provides a straightforward solution through its Site Dashboard. You can easily configure a primary domain for your site. This ensures that all traffic directed to non-primary domains is automatically redirected to your chosen primary domain. This method is ideal for enforcing canonical domain preferences (e.g., redirecting example.com and www.example.com to just example.com).

2. Implementing Page-Level Redirects in PHP:

For more granular control over redirects, particularly at the page level, Pantheon recommends using PHP within your site’s configuration files. Specifically:

  • WordPress: You can implement redirects in your wp-config.php file.
  • Drupal: Redirects can be configured in your settings.php file.

Here’s an example of how to implement a simple redirect in wp-config.php for WordPress:

if ( $_SERVER['REQUEST_URI'] == '/old-page/' ) {
  header( 'HTTP/1.1 301 Moved Permanently' );
  header( 'Location: https://example.com/new-page/' );
  exit();
}

And here’s a similar example for settings.php in Drupal:

if ($_SERVER['REQUEST_URI'] == '/old-path') {
  header('HTTP/301 Moved Permanently');
  header('Location: https://example.com/new-path');
  exit();
}

Advantages of PHP Redirects over .htaccess:

  • Application Awareness and Logic: PHP redirects offer significantly more flexibility. You can incorporate complex logic and make decisions based on application state, something impossible with .htaccess rules. This allows for conditional redirects, regular expressions, and integration with your application’s data.
  • Maintainability: For developers working with Drupal or WordPress, PHP is often a more familiar and maintainable language than Apache rewrite rules. Configuring redirects in settings.php or wp-config.php keeps the logic within the application’s codebase.
  • Performance Efficiency: Redirects implemented in settings.php and wp-config.php are processed very early in the website’s bootstrap process. This “early execution” makes them highly efficient with minimal performance overhead. Furthermore, when using 301 redirects, Pantheon’s Global CDN effectively caches these redirects, further enhancing performance and reducing server load.

Best Practices: Avoiding Excessive and Conflicting Redirects

When implementing multiple redirect rules, especially using PHP, it’s crucial to carefully consider the order and logic of your redirects. Avoid creating excessive redirect chains, as these can negatively impact user experience and SEO.

Key Considerations:

  • Logical Order: When combining general domain redirects with specific page redirects, ensure the order of execution is correct. For example, a broad redirect to a primary domain should generally be placed after specific page redirects to avoid inadvertently overriding them.
  • Step-by-Step Logic: Thoroughly review your redirect logic to ensure it behaves as intended, especially when using conditional statements (if conditions).
  • Testing: Always test your redirects thoroughly in a development or staging environment before deploying them to production.

By understanding Pantheon’s approach to server-level redirects and utilizing the recommended methods of primary domain settings and PHP redirects, you can effectively manage site traffic while maintaining performance and efficiency on the Pantheon platform.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *