How to Add Custom Redirects on Bluehost Cloud
Using the file's custom redirects on Bluehost Cloud Hosting, you can modify certain per-site request behaviors. This file is executed before any PHP script is accessed on the site, making it a powerful tool for custom redirects, security enhancements, and access controls. However, it is important to note that custom-redirects.php should be used sparingly. Solutions should constantly be tested thoroughly with page and edge caching in mind.
Recommendations
Before implementing changes via custom-redirects.php
, consider alternative solutions. If using it, be sure to:
- Test your implementation thoroughly, especially with page and edge caches in place.
- Avoid overuse of this file to prevent unnecessary complexity or performance issues.
Getting Started
- Create the custom-redirects.php file:
- Locate your site's
htdocs
directory. - Create a file named
custom-redirects.php
. - Ensure the file begins with the PHP opening tag
<?php
.
- Locate your site's
- Modify request behaviors:
- You can now add custom PHP code to modify the behavior of incoming requests to your site.
Best Practices and Considerations
- Testing: Be sure to carefully test any changes made via
custom-redirects.php
, particularly with caching systems like page cache and edge cache. Incorrect configurations may lead to unexpected results or performance issues. - Performance Impact: Because
custom-redirects.php
is executed on every request, overusing complex scripts or multiple redirects can negatively impact your site's performance. Always consider performance when using this file.
Add Custom Redirects
Below are several common use cases with examples for implementing custom-redirects.php
on your Bluehost Cloud site.
Basic Page Redirect
This simple redirect will send users to a new location if they visit a specific page.
Note: When setting up redirects, subdir refers to a specific subdirectory. For example, if /subdir is the default directory, you can redirect it to /subdir-new.
- /subdir: Original subdirectory.
- /subdir-new: Destination subdirectory for the redirect.
if ( $_SERVER['REQUEST_URI'] == '/subdir' ) { header('HTTP/1.1 301 Moved Permanently'); header('Location: /subdir-new'); exit; }
Add Security Headers
To enhance security, you can add various HTTP headers.
header('X-XSS-Protection: 1; mode=block'); header('X-Content-Type-Options: nosniff'); header('X-Frame-Options: SAMEORIGIN'); header('Referrer-Policy: no-referrer-when-downgrade');
Site Redirect
You can perform a site-wide redirect based on specific conditions, such as redirecting the homepage to a different URL.
if ( $_SERVER['HTTP_HOST'] == 'https://your-domain.site.com' && $_SERVER['REQUEST_URI'] == '/' ) { header('HTTP/1.1 301 Moved Permanently'); header('Location: https://externeral-url.com/news'); exit; }
Geoblocking by Country Code
Geoblocking restricts access via country code. This example only allows requests from the United States (US) and Canada (CA), bypassing the check for PHP-FPM
and CLI
requests.
// 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 == 'fpm-fcgi' || $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; }
Basic Access Block
To block access to a specific file or folder, you can use the following snippet, which returns a 410 Gone HTTP
status.
if ( strpos($_SERVER['REQUEST_URI'],'{file or folder name here, no curly braces}') !== false ) { http_response_code( 410 ); exit; }
Limiting Access by IP
You can use a script like the one below to restrict access to certain pages or sections of your site based on a visitor's IP address. It only allows access from specific proxy IPs.
/** * The following blocks the sandbox URI to anyone who isn't on a COMPANY_EXAMPLE_1 or COMPANY_EXAMPLE_2 Proxies * 123.456.789.101 is COMPANY_EXAMPLE_1's Proxy * 987.654.321.000 is COMPANY_EXAMPLE_2's Proxy */ $ips = array('123.456.789.101','987.654.321.000'); if ( strpos($_SERVER['REQUEST_URI'],'/sandbox') !== false ) { if ( !in_array($_SERVER['REMOTE_ADDR'],$ips) ) { header('HTTP/1.0 403 Forbidden'); echo '403 Forbidden'; exit; } }
Summary
While custom-redirects.php
offers a flexible way to modify request behavior on a Bluehost Cloud site, it should be used sparingly. Always ensure that any solution implemented is well-tested and does not conflict with caching mechanisms or other server settings. Explore alternative solutions or consult with our support team when in doubt.
If you need further assistance, feel free to contact us via Chat or Phone:
- Chat Support - While on our website, you should see a CHAT bubble in the bottom right-hand corner of the page. Click anywhere on the bubble to begin a chat session.
- Phone Support -
- US: 888-401-4678
- International: +1 801-765-9400
You may also refer to our Knowledge Base articles to help answer common questions and guide you through various setup, configuration, and troubleshooting steps.