Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess redirect append query string?

I have an htaccess redirect that needs to forward the query string to the new URL, but it's getting dropped after the redirect. Can someone tell me what's wrong?

RewriteRule ^services/agents.*$          https://services.example.com/agents/ [R=301,L,QSA]
like image 615
Aaron Avatar asked Feb 10 '26 20:02

Aaron


2 Answers

The same rule is working fine on my server. The problem should be something else. I added the same rule on my server and I get the following redirect

http://mysite.com/services/agents/foo?foo=bar => https://services.mysite.com/agents/?foo=bar

Please note that you don't need to add the QSA flag since the target doesn't include any query string. This article might contain some useful information to help you dealing with Htaccess and Query String.

like image 157
Simone Carletti Avatar answered Feb 14 '26 04:02

Simone Carletti


In general there is no need to explicitly append the query or use the QSA flag if you don’t specify a query for the substitution. But as you said your rule doesn’t work, try this:

RewriteRule ^services/agents.*$ https://services.example.com/agents/?%{QUERY_STRING} [R=301,L]
like image 20
Gumbo Avatar answered Feb 14 '26 05:02

Gumbo