.htaccess配置www跳转后URL自动追加/public_html/的问题求助
/public_html/ Redirect in Your .htaccess WWW Rule Hey there, let's troubleshoot why your www redirect is sending you to http://www.arnaldpedros.com/public_html/ instead of the clean root URL. This is a super common issue tied to how your server's document root is configured and where your .htaccess file lives—here's how to fix it, step by step:
First, Check Your .htaccess Location
Most hosting setups (like cPanel) set your website's document root to the public_html folder. If your .htaccess file is placed in the parent folder above public_html, the server will interpret paths relative to that parent directory, which is why it's adding /public_html/ to the redirect.
Fix: Move your .htaccess file directly into the public_html folder if it isn't already there.
Update Your Rewrite Rule for Reliability
Your original rule is close, but using $1 can sometimes pick up unintended path context if the server's base isn't set correctly. Try one of these more robust versions:
Option 1: Use %{REQUEST_URI} for Exact Path Matching
This uses the server's built-in variable to grab the exact requested path, avoiding any base path confusion:
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.arnaldpedros\.com$ [NC] RewriteRule ^ http://www.arnaldpedros.com%{REQUEST_URI} [L,R=301]
Option 2: Add RewriteBase / to Set the Root
If your server needs an explicit base path declared, adding RewriteBase / ensures all rewrite rules use the document root as the starting point:
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www\.arnaldpedros\.com$ [NC] RewriteRule ^(.*)$ http://www.arnaldpedros.com/$1 [L,R=301]
Critical Step: Clear Browser Cache
301 redirects are cached aggressively by browsers. Even after fixing the rule, you might still see the old /public_html/ path until you clear your browser's cache (or test in incognito mode to bypass cached redirects).
For Multiple Sites
Since you said this affects all your websites, check two things across all accounts:
- Every site's
.htaccessis in its respectivepublic_htmlfolder (the document root). - Your hosting panel (like cPanel) doesn't have conflicting redirect settings. Many hosts offer a built-in "Redirect to WWW" tool in the domain settings—using this can avoid manual
.htaccesspath issues entirely.
Bonus: Check for Rule Conflicts
If you have other rewrite rules in your .htaccess, make sure this www redirect rule is at the top of the file. Earlier rules might be modifying the request path before this rule runs, causing unexpected behavior.
内容的提问来源于stack exchange,提问作者piedracita

