如何通过根目录.htaccess将子域名重定向至指定文件?
Looks like the main issues with your current setup are missing subdirectory forwarding logic and incorrect path targeting in your redirect rule. Let's fix this step by step, keeping your requirement to keep the .htaccess in the public_html root directory.
Modified .htaccess Code
# -------------------------------------------------------------------------------------------------------------------------------------- # Block access to .htaccess file <Files .htaccess> order allow,deny deny from all </Files> # -------------------------------------------------------------------------------------------------------------------------------------- # Server Options Options All -Indexes +MultiViews # -------------------------------------------------------------------------------------------------------------------------------------- # Enable Rewrite Engine RewriteEngine On # -------------------------------------------------------------------------------------------------------------------------------------- # Add www prefix to main domain if missing RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC] # -------------------------------------------------------------------------------------------------------------------------------------- # Forward all app.example.com requests to its subdirectory RewriteCond %{HTTP_HOST} ^app\.example\.com$ [NC] RewriteCond %{REQUEST_URI} !^/app\.example\.com/ RewriteRule ^(.*)$ /app.example.com/$1 [L] # -------------------------------------------------------------------------------------------------------------------------------------- # Redirect app.example.com root to /dashboard/ RewriteCond %{HTTP_HOST} ^app\.example\.com$ [NC] RewriteRule ^$ /dashboard/ [L,R=301] # -------------------------------------------------------------------------------------------------------------------------------------- # Remove .php extension from URLs (works for both main domain and subdomain) RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+?)/?$ $1.php [L]
Key Changes & Explanations
Added Subdirectory Forwarding
This is the critical missing piece. Since yourdashboard.phplives inside thepublic_html/app.example.com/folder, we need to tell the server to route allapp.example.comrequests to this subdirectory. The two conditions ensure we only do this for the subdomain and don't create a loop.Optimized Redirect Rule
- Added
$to theHTTP_HOSTcondition to avoid matching unintended domains likeapp.example.com.xyz - Added
NC(case-insensitive) flag for broader compatibility - Changed the redirect target to
/dashboard/(with trailing slash) to match your desired URL structure and avoid extra processing from the extension-removal rule
- Added
Adjusted PHP Extension Rule
Replaced%{DOCUMENT_ROOT}/$1.php -fwith%{REQUEST_FILENAME}.php -fto make the rule work seamlessly with the subdirectory forwarding—this automatically looks for the PHP file in the correct subdirectory after forwarding.
Testing Steps
- Save the modified
.htaccessto yourpublic_htmlroot - Clear your browser cache (301 redirects are permanently cached by browsers)
- Visit
http://app.example.com—you should be automatically redirected tohttp://app.example.com/dashboard/and see the content fromdashboard.php
Notes
- Ensure your server has the
mod_rewritemodule enabled - Double-check your virtual host/DNS configuration to confirm
app.example.compoints to thepublic_htmldirectory - If testing in a browser still shows old behavior, try using an incognito/private window to bypass cached redirects
内容的提问来源于stack exchange,提问作者A Friend

