Skip to content

Always preserve query string on redirects#703

Merged
joseluisq merged 1 commit into
static-web-server:2.xfrom
urdh:preserve-query-strings
Jun 22, 2026
Merged

Always preserve query string on redirects#703
joseluisq merged 1 commit into
static-web-server:2.xfrom
urdh:preserve-query-strings

Conversation

@urdh

@urdh urdh commented Jun 20, 2026

Copy link
Copy Markdown

Description

Adds a new preserve-query option to redirects which causes SWS to append any query string received verbatim to the Location header when redirecting. This setting is optional, defaulting to false, which retains existing behavior for backward-compatibility.

Related Issue

#700

Motivation and Context

How Has This Been Tested?

Unit and integration tests have been written and pass (on Arch Linux, amd64, rustc 1.91.1).

Screenshots (if appropriate):

@semanticdiff-com

semanticdiff-com Bot commented Jun 20, 2026

Copy link
Copy Markdown

Review changes with  SemanticDiff

Changed Files
File Status
  src/redirects.rs  1% smaller
  tests/fixtures/toml/redirects.toml Unsupported file format
  tests/redirects.rs  0% smaller

@joseluisq joseluisq added enhancement New feature or request v2 v2 release labels Jun 21, 2026

@joseluisq joseluisq left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserving the query string for redirect is a good idea.
As I wrote in #700 (comment), we should add the query string if available by default to work out of the box.

IMO, this does not represent a "breaking change" per se, but it will enhance what users already have instead.

Here are some adjustments to make:

  • Change the master branch target to 2.x. As the master now points to the upcoming v3 (in development). Unless you really wanted your changes to be included in a future v3 release?
  • Let's include the query string in the redirect by default if present.
  • Let's adjust the tests so we assert this behaviour.
  • Revert the new option for redirect query support as we want it built-in.

Comment thread docs/content/features/url-redirects.md Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs have been moved to another repo. You can revert those changes here and make another PR mentioning this change in the new repo. You can find it at https://github.com/static-web-server/docs/tree/master/src/v2.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the PR for documentation changes: static-web-server/docs#6.

@urdh

urdh commented Jun 21, 2026

Copy link
Copy Markdown
Author

Sounds reasonable!

One thing I hadn't quite thought of when implementing this which might be more important to deal with if this changes default behavior rather than being opt-in is what do to when the destination already has a query string, e.g.:

[[advanced.redirects]]
source = "/index.html"
destination = "https://static-web-server.net/?src=some-value"
kind = 302

Apache has a QSA option for merging the query strings (the default is to always use the one from destination). There's also QSD for the case where destination doesn't have a query string, causing Apache to drop any source query strings (what SWS does now).

I'd lean toward a QSA-like behavior by default, what do you think?

@joseluisq

Copy link
Copy Markdown
Collaborator

Sounds reasonable!

One thing I hadn't quite thought of when implementing this which might be more important to deal with if this changes default behavior rather than being opt-in is what do to when the destination already has a query string, e.g.:

[[advanced.redirects]]
source = "/index.html"
destination = "https://static-web-server.net/?src=some-value"
kind = 302

Apache has a QSA option for merging the query strings (the default is to always use the one from destination). There's also QSD for the case where destination doesn't have a query string, causing Apache to drop any source query strings (what SWS does now).

I'd lean toward a QSA-like behavior by default, what do you think?

I believe QSA-like behavior by default is fine too if the query string is present, of course. Let's go with that then.

@urdh urdh force-pushed the preserve-query-strings branch from 1d794ab to 0e79439 Compare June 21, 2026 16:31
@urdh urdh changed the base branch from master to 2.x June 21, 2026 16:31
@urdh

urdh commented Jun 21, 2026

Copy link
Copy Markdown
Author

Great! I've updated the PR and re-targeted it to 2.x. Will try to send a documentation PR later this evening.

@urdh urdh changed the title Allow preserving query string on redirects Always preserve query string on redirects Jun 21, 2026

@joseluisq joseluisq left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A minor improvement to make.

Comment thread src/redirects.rs
@joseluisq joseluisq linked an issue Jun 21, 2026 that may be closed by this pull request
1 task
Updates the redirection logic to preserve query strings sent by
the client when performing redirects. The query string is appended
verbatim to the Location header after matching; if the destination
already has a query string the one from the client is appended. This
mimics the QSA rewrite option offered by Apache, and should be fairly
backward-compatible with existing configurations.

Closes #700
@urdh urdh force-pushed the preserve-query-strings branch from e52e0f9 to 83b1c25 Compare June 22, 2026 06:13

@joseluisq joseluisq left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok to me.
Thanks!

@joseluisq joseluisq merged commit b05bd25 into static-web-server:2.x Jun 22, 2026
34 checks passed
@joseluisq joseluisq added this to the v2.44.0 milestone Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request v2 v2 release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow preserving query string on redirects

2 participants