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

Apache URL模式重定向:.htaccess(mod_rewrite)规则调试需求

Fixing Your mod_rewrite Redirect for /fix/ URLs

First, let's break down why your existing rule isn't doing what you need:

  • Your current setup captures the path after /fix/ in %1, but the RewriteRule ^(.*) is matching the entire request URI (including /fix/), which creates unnecessary complexity here.
  • Most importantly, query parameters aren't being passed through—you haven't included the QSA flag, so any ?foo=bar at the end of the original URL gets dropped entirely.
  • While R=302 works for testing, once you confirm the redirect behaves as expected, you might want to switch to R=301 for permanent redirects (just remember to clear your browser cache after making that change).

Here's the corrected .htaccess code that handles both the path and query parameters correctly:

Options +FollowSymLinks
RewriteEngine On

# Redirect any URL starting with /fix/ to the subdomain, preserving path and query params
RewriteRule ^fix/(.*)$ http://server.sample.com/fix1/fix2/$1 [R=302,NC,QSA,L]

Let's walk through each part:

  • ^fix/(.*)$: This directly matches any request starting with /fix/, capturing everything after that into the $1 variable. We don't need a separate RewriteCond here—it's more efficient to handle the match in the rule itself.
  • http://server.sample.com/fix1/fix2/$1: We append the captured path ($1) to the target URL, so something like /fix/test/page becomes /fix1/fix2/test/page.
  • QSA: This critical flag ensures any query parameters from the original URL (like ?id=123) are appended to the target URL. So /fix/test?foo=bar becomes http://server.sample.com/fix1/fix2/test?foo=bar.
  • L: This tells mod_rewrite to stop processing further rules once this one matches, preventing unintended conflicts with other rules you might have in your .htaccess.
  • NC: Keeps the match case-insensitive, so /FIX/Test will redirect the same as /fix/test.

Quick Testing Tips:

If you're still running into issues:

  • Confirm mod_rewrite is enabled on your server (you can check via phpinfo() or reach out to your hosting provider if you're unsure).
  • Make sure your .htaccess file is in the root directory of sample.com (not inside the /fix/ subfolder).
  • Clear your browser cache before testing—old redirects can get cached and lead to confusing results.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:33:15