How to form redirect or rewrite where query string contains portion I want to extract to new query string
I've transferred an old custom-made site to Mediawiki and I have many internal and external links to my old pages that I want to redirect to the appropriate wiki page.
There are many links to the old site of the form:
https://www.example.com/?productOrPublisher=XXXXXX
with the wiki, the relevant page would be
https://www.example.com/index.php?title=XXXXXX
With all the attempts below, the URL remains ashttps://www.example.com/?productOrPublisher=XXXXXX
and the front page of the site always renders.
I've tried
RedirectMatch 301 "^?productOrPublisher=(.*?)$" "https://www.example.com/index.php?title=$1"
and
RewriteRule ^?productOrPublisher=(.*) https://www.example/index.php?title=$1 [L,R=permanent]
but these don't work. I'm not surprised, because I think I have to be extracting from%QUERY_STRING
somehow, but I don't know how.
I tried to adapt information from the 'Rewrite query string' section of https://httpd.apache.org/docs/2.4/rewrite/remapping.html for example this:
(.*(?:^|&))?productOrPublisher=(.*?)$
replacing with
index.php?title=$2
I tested this in notepad++ and there I can't get rid of the ?
after the /
so I get https://www.example.com/?index.php?title=FreeMind
and I tried it in .htaccess
but it leaves the URL unchanged.
There are many other redirects in .htaccess
and have placed these attempts near the top after RewriteEngine On
and RewriteBase /
to try to pre-empt others.
I think I have to be extracting from
%QUERY_STRING
somehow
Yes, you need to use mod_rewrite and check against the QUERY_STRING
server variable in a RewriteCond
(condition) directive.
And yes, these will need to go near the top of your .htaccess
file (without seeing your existing .htaccess
) to be sure there are no conflicts.
Try something like the following instead:
(I've assumed you are using Apache 2.4, as opposed to 2.2)
RewrietCond %QUERY_STRING ^productOrPublisher=([^&]+)
RewriteRule ^$ /index.php?title=%1 [R=302,L]
This matches a URL of the form /?productOrPublisher=XXXXXX
(where XXXXXX
is variable) and redirects to /index.php?title=XXXXXX
.
It won't match if there is a URL-path, or if there are any other URL parameters before this in the query string.
%1
is backreference to the last matched group in the preceding CondPattern.
This is also a 302 (temporary) redirect. Only change it to a 301 (permanent) redirect when you are sure it's working OK, since 301s are cached aggressively by the browser and so can make testing problematic.
Comments
Post a Comment