How to form redirect or rewrite where query string contains portion I want to extract to new query string

How to form redirect or rewrite where query string contains portion I want to extract to new query string - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the How to form redirect or rewrite where query string contains portion I want to extract to new query string error on your web browser. Problem :


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 as
https://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.


Solution :


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.


We hope that this article has helped you resolve the htaccess, redirects, query-string error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

Redirected urls show in serp, with the original permalink but with the title and meta of the target page

How can I redirect everything but the index as 410?

How do I redirect traffic only if being accessed from a specific port?