RW24
RW24

Reputation: 654

How to append word to URL

I have following URL:

https://example.com/nl/products/stones?color_id=13

I want it to be:

https://example.com/nl/products/stones/white-stones

So I have to delete the GET variable and append "white-stones" to the URL.

Currently I have tried this:

RewriteCond %{QUERY_STRING} ^color_id=[^&]+&?(.*) [NC]
RewriteRule ^ %{REQUEST_URI}?%1 [R=302,L]

Which outputted: https://example.com/nl/products/stones

But now I want to append "white-stones" to this result. How can I do this? In my research I found out that QSA should do that, but I can't seem to make it work.

Upvotes: 1

Views: 122

Answers (1)

MrWhite
MrWhite

Reputation: 45829

I have following URL: https://example.com/nl/products/stones?color_id=13 I want it to be: https://example.com/nl/products/stones/white-stones

If it's just this specific ID/URL then you presumably need to check for this specific ID and URL in your directive? This also greatly simplifies the requirement.

For example:

RewriteCond %{QUERY_STRING} ^color_id=13$
RewriteRule ^nl/products/stones$ %{REQUEST_URI}/white-stones [QSD,R=302,L]

The QSD flag (Apache 2.4+) discards the original query string from the request.

Or, could there be more query string parameters that you wish to maintain? Could this apply to any URL? This isn't explicitly mentioned in your question, but your directives hint at this. This would require a slightly different approach.

In my research I found out that QSA should do that

The QSA (Query String Append) simply appends (ie. merges) the query string from the original request with any query string you are assigning in the substitution. This would not seem to be what you require here.

Upvotes: 1

Related Questions