Many JS functions on my site are sending Ajax request to /?something=3213214 but I need to redirect them to /something=3213214

When I do:

RewriteRule ^test/(.*) /$1 [R]

It does redirect /test/something=3213214 to /something=3213214

But when I do:

# this probably works and redirect to the same
RewriteRule ^?(.*) /$1 [R]
RewriteRule ^\?(.*) /$1 [R]
RewriteRule ^/?(.*) /$1 [R]
RewriteRule ^/\?(.*) /$1 [R]

It does not redirect to /something=3213214

What is the right syntaxt for this kind of redirection?

1 Answer 1


Many JS functions on my site are sending Ajax request to /?something=3213214 but I need to redirect them to /something=3213214

In this case, you should really "fix" these AJAX requests. If that is not possible then you could perhaps implement a workaround as an internal rewrite - but that will depend on how the AJAX request is being routed.

Trying to externally "redirect" many such requests could cripple your site!? It may not even work, depending on how the AJAX request is implemented (it needs to expect a 3xx response and resend the request to the redirected URL).

With a request like /?something=3213214, everything after the first ? is the query string part of the URL. The RewriteRule pattern matches against the URL-path only, which notably excludes the query string. To match the query string you need to use a RewriteCond (condition) directive that matches against the QUERY_STRING server variable.

  1. RewriteRule ^?(.*) /$1 [R]
  2. RewriteRule ^\?(.*) /$1 [R]
  3. RewriteRule ^/?(.*) /$1 [R]
  4. RewriteRule ^/\?(.*) /$1 [R]

#1 and #2 would only match if the ? was URL encoded (%3F) in the URL. ie. It is part of the URL-path. Additionally, #3 and #4 would never match in a directory (.htaccess) context because of the slash prefix

To rewrite such a request you would need to do something like the following at the top of your .htaccess file:

RewriteCond %{QUERY_STRING} (.+)
RewriteRule ^$ /%1 [QSD,L]

However, this is very generic, as it matches any query string (as your question implies) and rewrites the request accordingly. You should be as specific as possible.

%1 (as opposed to $1) is a backreference to the first captured group in the last matched CondPattern.

But, as mentioned, whether this works at all will be dependent on the rest of your application... how your application routes these requests (and what other directives you have).


  • Thank you very much for your help, and for time You take to help. I've noticed some of your suggestions. But still no luck. I just can't get rid of ? from query string. I'm using PrestaShop. It's used to use /? as a begin of url for ajax requests, but for some weird reason it stops. But it does works with no ?, thats why I want to rewrite links.
    – bumerang
    Aug 21, 2019 at 15:05
  • And these AJAX requests are 3rd party requests to PrestaShop? Has something been updated? I don't know PrestaShop I'm afraid, but I would think there would be an option to determine the URL format that it accepts, such as "query string", "path-info" or "request-uri". /?something=3213214 is very different to /something=3213214 - it's not simply the absence of the ?. The way the application must read the request is potentially very different. (Many frameworks do have an option like this, to determine how such URLs are routed.)
    – MrWhite
    Aug 21, 2019 at 15:23
  • Yes, those are 3rd party functions. To be honest, it had happened after installation of one module (I suppose), but it did not change after uninstall and remove of it.
    – bumerang
    Aug 21, 2019 at 15:34
  • Man,you are life saviour,BIG upvote for rewriting starting question mark. Jul 24, 2022 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.