2

I have mediawiki installed inside mydomain.com/wiki/ folder and I also have phpbb installed in the root like mydomain.com. I already have user profile pages in my phpbb installation and the urls to those pages are like: mydomain.com/memberlist.php?mode=viewprofile&un=XYZ

What I want to do is, I dont want to use the Mediawiki's user pages and user talk pages. Instead I want to redirect all user pages to the phpbb's user profile page that is in the root directory. I tried to redirect using the htaccess method but couldnt get it to work. My htaccess inside the root folder has the following rewrite rule:

redirect 301 wiki/index.php/User:* http://www.mydomain.com/memberlist.php?mode=viewprofile&un=*

I need help with these:

1) Can someone have a look at the above code and let me know if I've got something wrong or if this cannot be done?

2) I have * next to the 'User:' since I want to transfer all existing Mediawiki User Pages to phpbb's memberlist.php page. Is that correct?

3) Also I noticed that in Mediawiki, the userpages have different types of URL. For example, sometimes its like: wiki/index.php/User:XYZ (or) wiki/index.php?title=User:XYZ (or) wiki/index.php?title=User:XYZ&action=edit&redlink=1, etc... So in that case, do I need to add different htaccess redirect rule for each type of urls's?

4) Is there another method like adding a redirect rule in LocalSettings.php or something else?

5) What is the right method for doing this?

I am stumped!

Neel
  • 9,352
  • 23
  • 87
  • 128

2 Answers2

0

Can someone have a look at the above code and let me know if I've got something wrong or if this cannot be done?

Yes, your Redirect directive is wrong. It isn't a regular expression and thus you can't have stuff like * in it, unless you're actually trying to match against a "*".

You're probably not going to be able to do the redirect from the htaccess file in your root directory as Mediawiki uses its own htaccess. So in the htaccess file that's in the /wiki/ folder, try adding this above any rewrite rules that are already there:

RewriteCond %{QUERY_STRING} ^title=User:([^&]+) [NC]
RewriteRule ^ /memberlist.php?mode=viewprofile&un=%1 [L,R=301]

That should take care of the URLs that look like: /index.php?title=User:XYZ

RewriteRule User:(.*)$ /memberlist.php?mode=viewprofile&un=$1 [L,R=301]

And that should take care of the URLs that look like: /User:XYZ

Is there another method like adding a redirect rule in LocalSettings.php or something else?

I don't know of anything you can set in the LocalSettings.php to do this kind of redirect, but I really wouldn't be surprised if there was some Mediawiki specific solution to this problem, maybe some add-on code that automatically redirects pages in a certain namespace.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thank you Jon... Sorry this may sound like a basic question to you... Since there is no default htaccess file in wiki folder, do I just create an empty htaccess file and put the above redirection in it? Do I need to add anything else to it for the redirection to work? is there a sample htaccess file I can download for wiki? Sorry I am pretty clueless.. – Neel Sep 11 '13 at 20:46
  • @blackops_programmer if there is no htaccess file in the Mediawiki folder, are there Mediawiki rules in the htaccess file of the document root? Or does all of your wiki pages look like: `/wiki/index.php?title=MainPage`? You want to add these rules where you have mediawiki rules, but just before them in the same file. – Jon Lin Sep 11 '13 at 20:57
  • Ah got it.. Atm I dont really have the mw rules in the root htaccess file. Then again, I am just testing in my localhost but it will be different once I install it on my live site this weekend. Also I guess I would probably will need to create an htaccess file then since I am also planning to add the short url method. Anyhow for now, I used an htaccess generator tool online to create a simple htaccess file for my localhost wiki folder and then added your given rewrite rule and it works beautifully. THANK YOU SO MUCH!! This is exactly what I was trying to achieve and your support was spot on!:) – Neel Sep 11 '13 at 21:13
  • Hi Jon, I just installed my wiki on my live site last night and I have been trying to get this rewrite rule to work. I installed the wiki in /w/ folder and using the short URL method, I am redirecting the main urls to /wiki/Page_Title as per the instructions given here: https://www.mediawiki.org/wiki/Manual:Short_URL/Apache When I add the above rewrite rule in my htaccess inside /w/ folder, it redirects url with ?title= but it doesnt redirect the urls with User:xyz since those urls are rewritten in root htaccess to wiki/User:xyz. How can I get this to work when short url is implemented please? – Neel Sep 17 '13 at 18:54
  • @blackops_programmer Remove the `,R=301` from the rewrite flags. – Jon Lin Sep 17 '13 at 19:14
  • Hi Jon, yeah it works when the url is like w/index.php/User:xyz. But if the url is based on the Short URL created such as wiki/User:xyz, then it doesnt work. But maybe I shouldnt be worrying about the wiki/User:xyz url since I am guessing all the users page urls within wiki will be either directing to w/index.php/User: or w/index.php?title=User:, is that right? – Neel Sep 17 '13 at 19:59
  • @blackops_programmer The rule `RewriteRule User:(.*)$ /memberlist.php?mode=viewprofile&un=$1 [L,R=301]` is supposed to take care of that. Since there is no `^` in the regex, the `User:xxxx` can by anywhere in the entire URI, and it will match. If it's not being applied, there's another rule that is being applied before that one is. All of the above rules need to be before any mediawiki rule that appears in your htaccess file – Jon Lin Sep 17 '13 at 20:12
  • Hi I had that rule in htaccess file inside my 'w' folder and thats why it was only redirecting the w/index.php/User: urls and not the wiki/User: urls. After your advise, I moved these rules to the root htaccess file and I added this before the Short URL rewrite rule and it works! Thanks again Jon. You have been incredibly helpful!! If you dont mind, would you be able to help me on 1 more rewrite rule plz? I am trying to redirect /wiki/ url to /wiki/Main_Page. I've posted it here: http://stackoverflow.com/questions/18843356/how-to-redirect-mediawiki-short-url-with-no-title-to-main-page – Neel Sep 17 '13 at 20:31
  • Hi Jon, Here is an update on this once again. This is what I found: The redirection for w/index.php/User:xyz and w/index.php?title=User:xyz only works when the above redirection rule in htaccess inside 'w' folder. This rewrite rule is at the beginning. And the redirection for wiki/User:xyz only works when the above redirection rule in htaccess inside root folder. Dont understand why that is. So for now, I have these rewrite rules in both root/.htaccess as well as root/w/.htaccess – Neel Sep 17 '13 at 21:36
  • Here is the final update on this. You have been right all along. It wasnt working due to a silly mistake from my end. You kept mentioning to add these rewrite rules before all other rewrite rules. I thought I did that and only today I noticed that there was 1 other rewrite rule before this that was causing an issue with this one. Once I added these rules right at the top, everything worked perfectly. So it was basically my stupidity and carelessness. Sorry for hassling you on this. Everything works perfect now. Thanks again Jon. :) – Neel Sep 18 '13 at 21:50
0

I think it's best to redirect those URLs in MediaWiki rather than in your webserver. Even better, let's set the target URL as canonical so that everyone knows where they'd better look in the first place.

Similar to the redirectless main page, use the GetLocalURL hook to run your code in LocalSettings.php. Something like the following.

$wgHooks['GetLocalURL'][] = function ( &$title, &$url, $query ) {
    $ns = $title->getNamespace();
    // Check for user namespaces but ignore subpages.
    if ( !$title->isSubpage() && ( $ns === 2 || $ns === 3 ) ) {
        $url = 'http://www.example.org/memberlist.php?mode=viewprofile&un=' . $title->getText();
    }
};
Nemo
  • 2,441
  • 2
  • 29
  • 63