Apache2下Bolt应用HTTPS跳转与/public/目录重定向问题排查
Let's break down what's going wrong here and fix your .htaccess step by step.
First, the core issues with your current setup:
- Rule order is reversed: You're trying to rewrite to
/public/before handling the HTTPS redirect, which can cause messy path conflicts or redirect loops. - Invalid condition in your HTTPS rule: The
RewriteCond %{REQUEST_URI} /public/$1doesn't work because$1is only defined in theRewriteRule(not in conditions), and this condition is unnecessary anyway. - No exclusion for
/public/requests: Without excluding requests that already target/public/, you risk creating a rewrite loop that triggers 404s or errors.
Here's the corrected, combined configuration that handles both requirements properly:
<IfModule mod_rewrite.c> RewriteEngine On # 1. Redirect all HTTP requests to HTTPS first RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA] # Block direct access to cache directory (keep your existing rule) RewriteRule cache/ - [F] # 2. Rewrite requests to /public/ ONLY if they aren't already targeting /public/ RewriteCond %{REQUEST_URI} !^/public/ RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.ch$ RewriteRule ^(.*)$ /public/$1 [PT,L,QSA] </IfModule>
Let's go through each change:
- HTTPS redirect first: We handle the HTTPS conversion before any path rewrites. This ensures all traffic is secure first, avoiding cases where a rewritten path gets stuck in an HTTP loop. Using
R=301marks this as a permanent redirect (good for SEO), andLtells Apache to stop processing further rules after this one if the condition is met. - Exclude
/public/from the rewrite: TheRewriteCond %{REQUEST_URI} !^/public/line ensures we don't rewrite requests that are already going to the/public/directory. This prevents infinite rewrite loops that would cause server errors or 404s. - Simplified host condition: Combined your two
HTTP_HOSTconditions into one using^(www\.)?to match bothmydomain.chandwww.mydomain.chin a single line. - Removed duplicate
RewriteEngine On: You only need to enable this once at the top.
Why your original HTTPS rule failed:
Your line RewriteCond %{REQUEST_URI} /public/$1 was invalid because $1 refers to a capture group from a RewriteRule, which doesn't exist in a RewriteCond. Even if it worked, checking for /public/ in the URI before redirecting to HTTPS would mean non-/public/ requests never got redirected to HTTPS, which isn't what you want.
Final checks:
- Make sure
mod_rewriteis enabled on your Apache server. - Verify that your hosting allows
.htaccessoverrides (check yourhttpd.confor virtual host config forAllowOverride All). - Clear your browser cache before testing, since old 301 redirects can be cached.
内容的提问来源于stack exchange,提问作者Peter
相关产品推荐
相关产品推荐

