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

Apache ModRewrite能否重写完整URL参数?含404问题咨询

Fixing Apache ModRewrite 404 When Rewriting Encoded Full URLs

Great question! Let's break down why you're seeing that 404 error when accessing http://example.com/go/http%3A%2F%2Ftarget.com%2Fpage%3Fid%3D8900, even though simpler paths like http://example.com/go/something/after/go work fine.

The Root Cause

Apache's default configuration blocks encoded slashes (%2F) in URL paths. When you pass a fully encoded URL like http%3A%2F%2Ftarget.com%2Fpage%3Fid%3D8900, those %2F get decoded to actual slashes behind the scenes. This makes Apache think you're trying to access a nested path like /go/http://target.com/page?id=8900—which doesn't exist, hence the 404.

Step 1: Allow Encoded Slashes in Apache Config

First, fix the server-level restriction. You'll need to add this line to your Apache main config (httpd.conf) or your virtual host's configuration file:

AllowEncodedSlashes NoDecode
  • Important: This setting can't be added to .htaccess—it has to go in the server or virtual host config. After adding it, restart Apache for changes to take effect.
  • The NoDecode flag tells Apache to leave the encoded slashes as-is, so ModRewrite can process the full encoded string instead of trying to treat it as a nested directory path.

Step 2: Adjust Your .htaccess Rewrite Rules

Next, make sure your rewrite rule in /go/.htaccess is set up to capture the full encoded URL. Let's assume your current rule looks something like this (adjust if your actual rule differs):

RewriteEngine On
RewriteRule ^go/(.+)$ your-handler-script.php?target=$1 [L,QSA]
  • The (.+) regex will match any character sequence after /go/, including all the encoded special characters like %3A (colon) and %3F (question mark).
  • The QSA (Query String Append) flag ensures any extra query parameters on the original request get passed through to your handler script, which is useful if you ever need to add more parameters alongside the encoded URL.

Testing the Fix

Once you've updated the Apache config and .htaccess, test the problematic URL again. Your handler script should now receive the full encoded string as the target parameter (or whatever you named it), and you can decode it in your code to get the original target URL.

Extra Tips

  • If you're on shared hosting and can't edit the Apache config, reach out to your hosting provider and ask them to enable AllowEncodedSlashes NoDecode for your site.
  • If your encoded URLs are extremely long, you might hit Apache's default request line limits. You can adjust LimitRequestLine and LimitRequestFieldSize in the Apache config if needed, but this is rarely necessary for most use cases.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:14:39