MediaWiki .htaccess Solution



This is the result of two hours of wrestling with .htaccess and trying to get clean URLs out of MediaWiki, while also setting up exclusions for things like robots.txt, favicon.ico, and a stats package I have installed outside of the wiki. I was never able to get any RewriteCond statements working on Dreamhost, so I found out how to do everything with RewriteRule statements and the final result is pretty clean. Here’s my full .htaccess for my testbed website

RewriteEngine on

RewriteRule ^statistics/$ statistics/index.php [L]
RewriteRule ^statistics/(.)$ statistics/$1 [L]
RewriteRule ^favicon.ico$ favicon.ico [L]
RewriteRule ^robots.txt$ robots.txt [L]
RewriteRule ^$ wiki/index.php?title=Main_Page [L]
RewriteRule ^wiki/(. )$ wiki/$1 [L]
RewriteRule ^(.
)$ wiki/index.php?title=$1 [L,QSA]

The first four rules are exceptions, which allow the statistics/ directory, favicon.ico, and robots.txt to be served up normally. From what I can tell each directory needs two rules, one to redirect from the directory name to a file inside of it (the index file inside of it I presume) and one to pass-through everything inside the directory. The next line cleans up the homepage; when you go to that URL stays in the address bar instead of redirecting you to /Main_Page or something to that effect. The last two rules are what make the clean URL rewriting work for MediaWiki, I copied these from somewhere else on the web but added the ,QSA which passes query strings through, so a URL like will work as expected.

The LocalSettings.php editing was straightforward. $IP points to the full file path of the wiki directory under my webroot. The other (possibly) relevant settings:

$wgScriptPath = “/wiki”;
$wgScript = “$wgScriptPath/index.php”;
$wgRedirectScript = “$wgScriptPath/redirect.php”;
$wgArticlePath = “/$1”;

I’ve also made a skin for MediaWiki that removes all formatting from the content, wraps the content in div tags and handles all formatting with a stylesheet. The approach I think they should have taken from the beginning, except for this weird fascination the developers have with supporting Netscape 4. I’ll leave that to another post though.

EDIT: For some reason if I have a link in my wiki to it just gets redirected to the main page, although typing that URL in works fine. Doesn’t affect me since I won’t be linking to that directory, but definitely worth noting.


Great job, .htaccess can be very frustrating… We certainly get our share of rewrite questions when dealing with the Wordpress / Gallery2 interactions…

Would you consider posting this into the DH Wik?

Wordpress Gallery2 Integration Community