By Roy Grubb

2019-03-10 13:28:07 8 Comments

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:

with the wiki, the relevant page would be

With all the attempts below, the URL remains as and the front page of the site always renders.

I've tried

RedirectMatch 301 "^\?productOrPublisher=(.*?)$" "$1"


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 for example this:


replacing with


I tested this in notepad++ and there I can't get rid of the ? after the / so I get 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.


@MrWhite 2019-03-10 14:44:03

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.

@Roy Grubb 2019-03-10 16:28:43

Confirmation that this works, thank you. It won't show my upvote as rep<15, but your help much appreciated. Been struggling for a couple of hours with this. Note to self: Must master backreference in RegEx.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How to Redirect Pages with Query String?

1 Answered Questions

[SOLVED] Redirect query string URL via .htaccess

2 Answered Questions

[SOLVED] Banning IPs through my htaccess doesn't work. Why?

  • 2014-03-09 18:53:24
  • Punct Ulica
  • 729 View
  • 1 Score
  • 2 Answer
  • Tags:   htaccess

1 Answered Questions

[SOLVED] Redirect URL with query string to homepage

  • 2013-12-30 23:33:59
  • user34853
  • 1280 View
  • 3 Score
  • 1 Answer
  • Tags:   htaccess redirects

1 Answered Questions

1 Answered Questions

[SOLVED] redirect using htaccess, if a url contains query string

  • 2012-01-07 14:50:30
  • user930026
  • 7082 View
  • 1 Score
  • 1 Answer
  • Tags:   wordpress htaccess

2 Answered Questions

[SOLVED] Where is the query string pagewanted=all coming from?

Sponsored Content