What is the best way to create 301 redirects on Pantheon for file? I can create redirects for paths by placing rules in my settings.php:

switch ($_SERVER['REQUEST_URI']) {
  case '/my-path':
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://mydoamin/my-new-path');

This is the redirect I want to create:

a/b/c/document.pdf and e/f/g/document2.pdfboth should be redirected to /new/folder/document3.pdf.

Unfortunately, this won't work with files.

Here is how I test:

  1. Create file test.txt in sites/default/files

  2. Access sites/default/files/test.txt and verify it works

  3. Update settings.php and add die($_SERVER['REQUEST_URI']); and verify you the get uri.

Instead of getting URI I get 404.

1 Answer 1


Thanks to Sarah at Pantheon for walking me through this.

File redirects aren't really possible using the same logic used to do PHP redirects for URLs. Requests to files are managed by the server and don't run through settings.php (Drupal) or wp-config.php (WP). It is not possible to intercept those like a URL served by the application.

If you were running your own nginx server you could manage this in a .conf file, but on Pantheon the nginx.conf isn't editable on a per-site basis since it's set platform-wide (https://pantheon.io/docs/platform-considerations/#nginxconf)

A workaround could be to utilize a third-party CDN like Cloudflare to create nginx-like redirects before you hit the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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