On this page
A CDN URL rewrite may preserve the incoming query string, replace it, or remove it. The result depends on the CDN product and the rule you configure. Separately, the cache key may include all, some, or none of those parameters—even when they are forwarded to the origin.
This guide shows how to compare the incoming URL, rewritten URL, cache key, and origin request in a local lab, then apply the same troubleshooting approach to a CDN configuration.
Key Takeaways
- Do not assume a path rewrite preserves or discards query parameters: check the product documentation and the deployed rule.
- The URL sent to the origin and the cache key are different things. A parameter can reach the origin without distinguishing cached responses.
- Test repeated parameters, blank values, and encoded characters using the raw request target as well as any parsed representation.
- If a parameter changes the response, omitting it from the cache key can cause one request to receive a response generated for another.
Four things to compare
Consider a request for /shop?category=shoes&utm_source=mail. A routing rule might change the path to /catalog while keeping the query string. A separate cache policy might include category but ignore utm_source.
| Item | Example in that configuration | Why it matters |
|---|---|---|
| Incoming request target | /shop?category=shoes&utm_source=mail | What the CDN receives from the client. |
| Rewritten request target | /catalog?category=shoes&utm_source=mail | What the routing rule produces. |
| Cache key | /catalog?category=shoes | Which requests are treated as equivalent for caching. This is a simplified example, not a complete CDN key. |
| Origin request target | /catalog?category=shoes&utm_source=mail | What the origin actually receives on a request forwarded to it. |
These are examples, not defaults. A CDN may construct its key from a different stage of processing, and a real key can also depend on the hostname, headers, cookies, or other configuration.
If category changes the product listing but is omitted from the cache key, a cached shoes page could be served for another category. Conversely, excluding a campaign parameter such as utm_source can be appropriate only if it does not affect the cached response, including any response headers or cookies. Forwarding a parameter to the origin does not by itself make it part of the cache key.
Advertisement
Reproduce the distinction locally
This Python lab simulates an edge rewrite and a separate cache-key policy. It does not emulate a particular CDN, implement a cache, or establish any provider's behavior. The example has not been executed here; run it locally to inspect the results.
You need Python 3 and curl. No credentials or external service are required. Save the following as rewrite_lab.py:
from http.client import HTTPConnection
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from threading import Thread
from urllib.parse import parse_qsl, urlencode
ORIGIN_PORT = 8081
EDGE_PORT = 8080
KEY_PARAMETERS = {"category", "tag", "empty", "q"}
class OriginHandler(BaseHTTPRequestHandler):
def do_GET(self):
body = f"Origin received request target: {self.path!r}\n".encode()
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
class EdgeHandler(BaseHTTPRequestHandler):
def do_GET(self):
path, separator, raw_query = self.path.partition("?")
if path != "/shop":
self.send_error(404, "Lab accepts only /shop")
return
# Preserve the raw query in the request forwarded to the origin.
origin_target = "/catalog" + ("?" + raw_query if separator else "")
# Compute a hypothetical cache key independently. No cache is used.
pairs = parse_qsl(raw_query, keep_blank_values=True)
key_pairs = [(name, value) for name, value in pairs
if name in KEY_PARAMETERS]
key_query = urlencode(key_pairs)
cache_key = "/catalog" + ("?" + key_query if key_query else "")
print(f"Incoming: {self.path!r}", flush=True)
print(f"Rewritten: {origin_target!r}", flush=True)
print(f"Cache key: {cache_key!r} (hypothetical)", flush=True)
print(f"Forwarding: {origin_target!r}", flush=True)
connection = HTTPConnection("127.0.0.1", ORIGIN_PORT, timeout=3)
try:
connection.request("GET", origin_target)
response = connection.getresponse()
body = response.read()
self.send_response(response.status)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
except OSError:
self.send_error(502, "Local origin unavailable")
finally:
connection.close()
origin = ThreadingHTTPServer(("127.0.0.1", ORIGIN_PORT), OriginHandler)
edge = ThreadingHTTPServer(("127.0.0.1", EDGE_PORT), EdgeHandler)
origin_thread = Thread(target=origin.serve_forever, daemon=True)
origin_thread.start()
try:
print(f"Lab listening on 127.0.0.1:{EDGE_PORT}", flush=True)
edge.serve_forever()
except KeyboardInterrupt:
pass
finally:
edge.server_close()
origin.shutdown()
origin.server_close()
origin_thread.join()
Start the lab in one terminal:
python3 rewrite_lab.py
In another terminal, send these requests:
curl -i 'http://127.0.0.1:8080/shop?category=shoes&utm_source=mail'
curl -i 'http://127.0.0.1:8080/shop?category=shoes&utm_source=social'
curl -i 'http://127.0.0.1:8080/shop?tag=red&tag=blue&empty=&q=a%26b'
curl -i 'http://127.0.0.1:8080/shop?tag=blue&tag=red&empty=&q=a%26b'
Compare the edge terminal's four lines for each request with the origin request target in the response. The first two requests should have different forwarded campaign values but the same hypothetical key, because the lab excludes utm_source. The repeated tag parameters and blank empty value are retained in the key; the origin receives the raw query. The encoded %26 is part of the q value rather than a parameter separator.
The lab parses and re-encodes the query when building its key, so the key need not use the same textual encoding as the forwarded query. It also preserves the order of included pairs: the two tag orderings produce different hypothetical keys. That is a lab choice, not a universal CDN rule. Stop the server with Ctrl+C. If a port is already in use, change the corresponding port constant and the request URLs.
Advertisement
Check a real CDN configuration
For a concrete product, use Cloudflare as the verification target—but do not infer Cloudflare's rewrite, cache-key, or forwarding behavior from the lab. Its official documentation was not available to consult in this run, so current product-specific behavior has not been verified here. Before publishing or deploying a rule, consult the current official documentation for the exact rule type and cache-key configuration in use, and confirm the behavior in a staging environment.
Use a test hostname and a controlled origin that can record the raw request target. Avoid production credentials, personal data, and real campaign identifiers. Then:
- Record the incoming request. Send a known path and query with
curl, quoting the URL so the shell does not interpret&. - Inspect the rewrite configuration. Determine whether the rule modifies only the path, explicitly supplies a query, or uses another operation. Check rule ordering and any later transformations.
- Observe the origin request. On a request known to reach the origin, compare the raw target with the input. An application framework's parsed parameter object alone may hide differences in encoding or repeated keys.
- Inspect the cache policy separately. Identify which query parameters and other request attributes distinguish entries. Confirm whether the key is based on the incoming or transformed request and whether normalization occurs.
- Test a cache hit as well as a miss. Use an approved way to isolate or invalidate test entries. Change one response-affecting parameter at a time and verify both the response and whether the origin was contacted. An origin log cannot show a request served entirely from cache.
Repeat with tag=red&tag=blue, empty=, and q=a%26b. Also compare parameter order, a missing parameter versus an empty one, and + versus percent-encoded spaces if your application distinguishes their raw forms. Record both the raw target and how the origin interprets it.
A rewrite is not necessarily a redirect: a redirect tells the client to make another request, while an internal rewrite can change what is routed or forwarded without changing the address shown in the browser. Diagnose the actual configured operation.
Common Mistakes
- Checking only the origin log. A cache hit may never reach the origin, so origin logging alone cannot establish cache behavior.
- Treating forwarding and cache inclusion as the same setting. A query can be forwarded while the cache ignores it, or a provider may configure these operations differently.
- Testing only a single parameter value. Repeated keys, blank values, ordering, and encoding can expose differences that a simple query misses.
- Ignoring application semantics. If the origin personalizes content based on a query, cookie, authorization state, or header, the cache policy must account for it or avoid shared caching for that response.
- Generalizing from one CDN. Rule syntax, processing order, normalization, and defaults are product- and configuration-dependent.
Best Practices
Document the intended origin request and cache-key inputs alongside each rewrite. Include every input that can change a shared cached response, or configure that response so it is not shared. Use controlled staging tests after rule changes, and avoid logging sensitive query values: URLs can contain tokens or personal information.
Conclusion
A rewrite and a cache key answer different questions: what request is routed, and which requests may share a cached response. Verify both independently, then confirm the actual origin request and cache-hit behavior for your CDN and application.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.