Plesk中子域名指向文件夹配置问题求助
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:
- Log into your Plesk panel.
- Go to Domains > Click Add Domain.
- Select the Subdomain option, then enter
subin the subdomain field (it will auto-populatesub.domain.com). - 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/subfolder (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).
- If you want the subdomain to directly point to
- 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 theHTTP_HOSTcondition 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/subfolder).
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
/subfolder (e.g.,sub.domain.com/test.htmlshould 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 (usually755for folders and644for files, with the owner set to the Plesk web user likeftpuserorwww-data). - Enable mod_rewrite: Double-check that Apache's
mod_rewriteis enabled in Plesk. Go to Domains > Your main domain > Hosting Settings and make sure the "Apache mod_rewrite" option is checked.
Final Testing Steps
- Clear your browser cache (or use incognito mode) to avoid cached redirects.
- Visit
sub.domain.com—it should now load content from your/subfolder instead of the Plesk login page. - Test a specific file (e.g.,
sub.domain.com/index.html) to confirm it loads without a 404.
内容的提问来源于stack exchange,提问作者Javi

