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

Plesk中子域名指向文件夹配置问题求助

Fixing Subdomain to Folder Redirect in Plesk with .htaccess

Hey Javi, let's work through this issue step by step—your problem is super common with Plesk because it has its own virtual host configuration that takes priority over .htaccess rules, which is why you're seeing the Plesk login page instead of your folder content.

First: Fix the Plesk Subdomain Configuration (Critical!)

The biggest issue here is that Plesk doesn't recognize sub.domain.com as a valid site yet. When you hit that subdomain and get redirected to the Plesk login page, it means Plesk is routing the request to its default catch-all instead of your website's directory. Here's how to fix that:

  1. Log into your Plesk panel.
  2. Go to Domains > Click Add Domain.
  3. Select the Subdomain option, then enter sub in the subdomain field (it will auto-populate sub.domain.com).
  4. Under Hosting Settings, set the Document Root to either:
    • If you want the subdomain to directly point to /sub: Enter the full path to your /sub folder (e.g., /var/www/vhosts/domain.com/httpdocs/sub). This way, you might not even need the .htaccess rule later, but it depends on your setup.
    • If you want to keep the document root the same as your main domain (and use .htaccess to route): Leave it as the default main domain root (e.g., /var/www/vhosts/domain.com/httpdocs).
  5. Save the settings and wait a minute for Plesk to apply the virtual host configuration.

Verify Your DNS CNAME Record

Your CNAME record setup is actually correct—since you're able to reach the Plesk page, the DNS is resolving properly. No changes needed here unless you notice delays in propagation (but that's unlikely if you're already seeing the Plesk page).

Adjust Your .htaccess Rules

Once Plesk recognizes the subdomain, let's tweak your .htaccess to make the redirect work reliably:

Updated .htaccess Code

RewriteEngine On

# Redirect HTTP and WWW to HTTPS without WWW
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

# Redirect subdomain to folder (only if document root matches main domain)
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
# Prevent infinite redirect loops by skipping requests already targeting /sub
RewriteCond %{REQUEST_URI} !^/sub/
RewriteRule ^(.*)$ /sub/$1 [L,NC,QSA]

Key Changes:

  • Added $ to the end of the HTTP_HOST condition to avoid accidental partial matches.
  • Added a second condition RewriteCond %{REQUEST_URI} !^/sub/ to stop infinite redirect loops (this prevents the rule from reprocessing requests that are already pointing to the /sub folder).

Troubleshoot the 404 Error

If you still get 404s when accessing files in /sub:

  • Check file paths: Confirm the files you're trying to access actually exist in the /sub folder (e.g., sub.domain.com/test.html should map to /sub/test.html).
  • Verify folder permissions: In Plesk's File Manager, navigate to /sub, right-click it, and select Change Permissions. Ensure permissions are set to allow Apache to read files (usually 755 for folders and 644 for files, with the owner set to the Plesk web user like ftpuser or www-data).
  • Enable mod_rewrite: Double-check that Apache's mod_rewrite is enabled in Plesk. Go to Domains > Your main domain > Hosting Settings and make sure the "Apache mod_rewrite" option is checked.

Final Testing Steps

  1. Clear your browser cache (or use incognito mode) to avoid cached redirects.
  2. Visit sub.domain.com—it should now load content from your /sub folder instead of the Plesk login page.
  3. Test a specific file (e.g., sub.domain.com/index.html) to confirm it loads without a 404.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 17:45:38