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

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/$1 doesn't work because $1 is only defined in the RewriteRule (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=301 marks this as a permanent redirect (good for SEO), and L tells Apache to stop processing further rules after this one if the condition is met.
  • Exclude /public/ from the rewrite: The RewriteCond %{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_HOST conditions into one using ^(www\.)? to match both mydomain.ch and www.mydomain.ch in 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_rewrite is enabled on your Apache server.
  • Verify that your hosting allows .htaccess overrides (check your httpd.conf or virtual host config for AllowOverride All).
  • Clear your browser cache before testing, since old 301 redirects can be cached.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:31:25