I am trying to redirect
"/w/index.php?title=TITLE_HERE&oldid=100"
to
"/wiki/TITLE_HERE.php".
I tried several things, but the following RedirectMatch should work based on my research. I read quite a bit about rewriting in the different ways in Apache. The problem, of course, is that it does not work. I am not redirected at all when I visit
"/w/index.php?title=Example_Article&oldid=100".
This is what I'm trying:
RedirectMatch "^/w/index\.php?title=([^&] )" "/wiki/$1.php"
I have many other, working redirect/rewrite rules. I don't understand why this one doesn't work.
I tried with and without surrounding quotes. I also tried to add ".*$" at the end, in case a complete match is required, but no luck.
The character class [^&] means one instance of any character that is NOT '&'. Then I want one or more of those with the " " quantifier (greedy matching, of course). This should make it cut out the "&oldid=100" and make things smooth. But the redirect isn't even triggering.
I also tried "(. )" to see if the negated regex character class wasn't supported, but was not redirected then either.
By the way, my site is NOT a wiki. It used to be. Now it is static HTML. And this is why the redirects would be beneficial. I know you can rewrite the URLs differently in for instance MediaWiki.
(This is my first Stack Overflow question, so bear with me.)
CodePudding user response:
Add this to the top of any other rewrite rules (below the internal redirect rewrite-rules (those without the flag [R])):
RewriteCond %{REQUEST_URI} ^\/w\/index\.php$
RewriteCond %{QUERY_STRING} (?:^|&)title=(.*)(?:&|$)
RewriteRule ^ /wiki/%1.php [R=302,L]
If you are confused, the difference between $1 and %1 is: %1 gets the previous captured string from RewriteCond, and $1 is just like the usual RegEx back-reference, and only contains stuff from the current RewriteRule.
CodePudding user response:
This works for me:
RewriteCond %{REQUEST_URI} ^/w/index\.php$
RewriteCond %{QUERY_STRING} (?:^|\?)title=([^&] )
RewriteRule ^ /wiki/%1.php
Thanks for the expedient help.
Using "(. )(&|$)" at the end did not work for me. Page not found, and no redirect.
NB! The URL in the browser bar does not change with this method during my testing. But the correct article content shows. Haven't seen this before. With my other redirects, it updates the browser bar URL. NB2! With R=302, permanent redirect, it updates the browser URL bar. I was warned about permanent redirects during testing.
