<?php class ProRedirect { /** * Safely redirects to a specific URL. * * @param string $url The target URL. * @param int $statusCode HTTP status code (301, 302, 307, 308). * @param bool $exit Stop script execution immediately. */ public static function to($url, $statusCode = 302, $exit = true) { // 1. Security: Sanitize URL to prevent "Open Redirect" vulnerabilities // We only allow absolute HTTP/HTTPS URLs or relative paths. $url = filter_var($url, FILTER_SANITIZE_URL); // Prevent redirecting to a different domain if you only want internal redirects // Uncomment the next 3 lines to enforce internal-only redirects // $host = parse_url($url, PHP_URL_HOST); // if ($host && $host !== $_SERVER['HTTP_HOST']) { // die("Invalid redirect target."); // } // 2. Validate and Set HTTP Status Code // 301: Moved Permanently (SEO) // 302: Found (Temporary) // 307: Temporary Redirect (Preserves POST data on modern browsers) // 308: Permanent Redirect (Preserves POST data) $validCodes = [301, 302, 303, 307, 308]; if (!in_array($statusCode, $validCodes)) { $statusCode = 302; // Default to 302 if invalid } // 3. Check if headers have already been sent if (headers_sent($file, $line)) { // Fallback: If headers sent, we cannot use header(). Use JS/Meta. // This is critical for plugins/themes that output content early. echo ''; echo ''; if ($exit) die("Redirect failed (Headers already sent in $file on line $line)."); return; } // 4. Execute Native Header Redirect header("Location: " . $url, true, $statusCode); // 5. Optional: Add No-Index headers for temporary redirects if ($statusCode === 302 || $statusCode === 307) { header("X-Robots-Tag: noindex, nofollow", true); } if ($exit) exit; } } // --- USAGE EXAMPLES --- // Example 1: Standard Temporary Redirect ProRedirect::to("https://www.google.com"); // Example 2: SEO Permanent Redirect // ProRedirect::to("https://www.new-site.com", 301); // Example 3: Redirect after form submission (keeps POST data) // ProRedirect::to("/success-page", 307); ?>

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!


Comments

One response to “Hello world!”

  1. Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

Leave a Reply to A WordPress Commenter Cancel reply

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