Although there are plugins (such as Redirection) that can help you create redirects on your site, there is a way to have more (programmatic) control over redirects on our platform: custom-redirects.php.
custom-redirects.php is called before our must-use page caching plugin (Batcache), the PHP rendered in this file is exempt from page caching by default. In other words, using $_SERVER['GEOIP_COUNTRY_CODE'] or another variable, which will vary from user-to-user, won’t be cached in custom-redirects.php, but it would be cached if used in a plugin or a theme.
To implement redirects on a Pressable site without using a plugin, first, you need to add the custom-redirects.php file in your site’s web root (htdocs) by using your SFTP or SSH access. First, connect to the site, change the directory to htdocs and then create or place the custom-redirects.php file there.
Once you have created the file, you must add your PHP code.
Examples
Below are some example code snippets that can be used in custom-redirects.php.
Note: Don’t forget to add an opening <?php PHP tag to your code.
Page Redirect
You want to redirect https://test.mystagingwebsite.com/subdir to https://test.mystagingwebsite.com/subdir-new. In this case, you could insert the following code into custom-redirects.php:
if ( $_SERVER['REQUEST_URI'] == '/subdir' ) { header('HTTP/1.1 301 Moved Permanently'); header('Location: /subdir-new'); exit; } Site Redirect
You want to redirect a root domain or sub-domain for ex https://your-domain.site.com to an external URL https://externeral-url.com/news. In this case, you could insert the following code into custom-redirects.php:
if ( $_SERVER['HTTP_HOST'] == 'your-domain.site.com' && $_SERVER['REQUEST_URI'] == '/' ) { header('HTTP/1.1 301 Moved Permanently'); header('Location: https://externeral-url.com/news'); exit; } Geographic Blocking by Country Code
In the example below, we’ll restrict access via country codes (Alpha-2 ISO 3166-1 country codes from MaxMind’s DB). Specifically, this example allows access from the United States and Canada while also permitting requests from CLI.
/** * GEOGRAPHIC ACCESS RESTRICTION * * This segment of code is designed to restrict access to the website based on the geographic location of the user. * It checks the server API to ensure that the geographic checks are only applied in web contexts, not CLI or FPM. * Allowed countries are defined in the $allowedCountries array. Requests from other countries are blocked with a 404 response. * * Dependencies: Requires server variables set by the GeoIP module. * * @author Joshua Goode, Automattic * @date 2024-04-29 * @version 1.0.0 */ // Array of allowed country codes $allowedCountries = ['US', 'CA']; // Get the current server API $api = php_sapi_name(); // Bypass geo checks for non-web server APIs if ($api == 'cli') { return; // Early exit for CLI or FPM contexts } // Retrieve the country code or default to blocking access $countryCode = $_SERVER['GEOIP_COUNTRY_CODE'] ?? 'Unknown'; // Using null coalescing operator for clarity // Block access if the country code is not allowed if (!in_array($countryCode, $allowedCountries)) { header('HTTP/1.1 404 Not Found', true, 404); exit; } // End of Geographic Access Restriction code Custom Headers
You may also use custom-redirects.php to add custom headers to PHP requests. Visit our
Adding Custom Headers to Your WordPress Site guide for more on this usage.