I want https://www.example.com/somePage.php?id=1234
to be redirected to https://www.example.com/tool/1234
(so that legacy links and bookmarks work).
I think I am close, but this appears to not work...
RewriteRule ^somePage.php?toolid=([0-9]+)$ /tool/$1 [R=301,L]
What's wrong here?
You can't match the query string in the
RewriteRule
pattern. You need to use aRewriteCond
directive and compare against theQUERY_STRING
server variable.But also, assuming you have an internal rewrite later in your config file that rewrites back to the (real) ugly URL then you can't do a simple redirect, as it will result in a redirect loop. You need to make sure you only redirect the initial request, not the rewritten URL.
Assuming this is
.htaccess
, based on your existingRewriteRule
pattern, try the following, near the top of your config file:Note, however, that your example URLs differ from your code example. I've gone with your code example.
The
REDIRECT_STATUS
environment variable is empty on the initial request, but gets set to "200" after a successful internal rewrite. So, this avoids a redirect loop in per-directory.htaccess
files.Make sure you've cleared your browser cache, since any erroneous 301s (whilst testing) will have been cached by the browser.