如何通过.htaccess向PHP传值并指向返回内容及自定义域名重写规则
Got it, let's break down your two questions one by one—happy to help you sort this out!
Sending data to a PHP file and routing requests to it via .htaccess typically involves combining HTTP request methods (GET/POST) with rewrite rules for cleaner URLs. Here's how to do it:
Option 1: Send data via GET requests (with friendly URLs)
First, create a PHP file to handle the incoming data (e.g., data-handler.php):
<?php // Safely retrieve GET parameters (fallback to default if missing) $inputData = $_GET['user_data'] ?? 'no data provided'; // Return processed content echo "Received GET data: " . htmlspecialchars($inputData); ?>
Then add rewrite rules to .htaccess to map friendly URLs to this PHP file:
RewriteEngine On # Map URLs like /send-data/hello to data-handler.php?user_data=hello RewriteRule ^send-data/([a-zA-Z0-9_-]+)$ data-handler.php?user_data=$1 [L]
Now when users visit yourdomain.com/send-data/hello, the request will route to your PHP file, which returns the processed content.
Option 2: Send data via POST requests (for forms/AJAX)
If you're using a form or AJAX to send sensitive data, use POST. First, create a PHP handler (e.g., submit-handler.php):
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Retrieve POST data $username = $_POST['username'] ?? ''; $email = $_POST['email'] ?? ''; // Return confirmation or processed data echo "Thanks for submitting, " . htmlspecialchars($username) . "!"; } ?>
Add .htaccess rules to route form submissions to this file:
RewriteEngine On # Map /submit-form to submit-handler.php RewriteRule ^submit-form$ submit-handler.php [L]
You can then point your form to the friendly URL:
<form action="/submit-form" method="POST"> <input type="text" name="username" placeholder="Your name"> <input type="email" name="email" placeholder="Your email"> <button type="submit">Submit</button> </form>
Since your domain-to-directory mapping is stored in a PHP file, you can't directly read that data with .htaccess alone (unless you have server-level access for RewriteMap, which most shared hosts don't allow). Instead, use a PHP entry point to handle the routing:
Step 1: Ensure all custom domains point to your server
First, ask users to point their custom domain's DNS records to your server's IP. Then configure your web server to route all these custom domains to your main site's root directory.
Step 2: Create a PHP router file (e.g., domain-router.php)
This file will check the incoming domain, match it to your stored mapping, and load the corresponding user directory content:
<?php // Your domain-to-directory mapping (can also load from a database/config file) $domainMap = [ 'someusersite.com' => 'bob', 'anotheruserdomain.co' => 'test' ]; // Get the current request's domain and path $currentDomain = $_SERVER['HTTP_HOST']; $requestPath = $_SERVER['REQUEST_URI']; // Check if the domain exists in your mapping if (isset($domainMap[$currentDomain])) { $userDir = $domainMap[$currentDomain]; $targetPath = $_SERVER['DOCUMENT_ROOT'] . '/' . $userDir . $requestPath; // Serve the target content if it exists if (file_exists($targetPath)) { // Handle PHP files by including them if (pathinfo($targetPath, PATHINFO_EXTENSION) === 'php') { include $targetPath; } else { // Serve static files (images, HTML, CSS) with correct MIME type $mime = mime_content_type($targetPath); header("Content-Type: $mime"); readfile($targetPath); } } else { // Return 404 if the requested file doesn't exist http_response_code(404); echo "404 - Page not found"; } } else { // Return forbidden if the domain isn't registered http_response_code(403); echo "Forbidden: This domain isn't mapped to any user directory"; } ?>
Step 3: Configure .htaccess to route all requests to the router
Add these rules to your main site's .htaccess to send every request through the PHP router:
RewriteEngine On // Skip the router file itself to avoid infinite loops RewriteCond %{REQUEST_URI} !^/domain-router.php$ // Route all other requests to the router RewriteRule ^(.*)$ domain-router.php [L]
Now when someone visits someusersite.com/page, the request will go through domain-router.php, which loads the content from yourdomain.com/bob/page and returns it to the user.
内容的提问来源于stack exchange,提问作者Osman

