You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过根目录.htaccess将子域名重定向至指定文件?

Fixing app.example.com Root Redirect to /dashboard/

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

  1. Added Subdirectory Forwarding
    This is the critical missing piece. Since your dashboard.php lives inside the public_html/app.example.com/ folder, we need to tell the server to route all app.example.com requests to this subdirectory. The two conditions ensure we only do this for the subdomain and don't create a loop.

  2. Optimized Redirect Rule

    • Added $ to the HTTP_HOST condition to avoid matching unintended domains like app.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
  3. Adjusted PHP Extension Rule
    Replaced %{DOCUMENT_ROOT}/$1.php -f with %{REQUEST_FILENAME}.php -f to make the rule work seamlessly with the subdirectory forwarding—this automatically looks for the PHP file in the correct subdirectory after forwarding.

Testing Steps

  1. Save the modified .htaccess to your public_html root
  2. Clear your browser cache (301 redirects are permanently cached by browsers)
  3. Visit http://app.example.com—you should be automatically redirected to http://app.example.com/dashboard/ and see the content from dashboard.php

Notes

  • Ensure your server has the mod_rewrite module enabled
  • Double-check your virtual host/DNS configuration to confirm app.example.com points to the public_html directory
  • If testing in a browser still shows old behavior, try using an incognito/private window to bypass cached redirects

内容的提问来源于stack exchange,提问作者A Friend

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:25:33