.htaccess, mod_rewrite, PHP-CGI, and all that jazz



Like several people here, apparently, I’ve been battling with trying to get mod_rewrite rules embedded in a .htaccess file to do what I expect/want (or indeed anything), while installing third-party scripts under PHP-CGI. By sheer luck I’ve managed to get what I want working. The following is presented in the hope that my blind stumblings might help others.

Just enough mod_rewrite to get by

My goal was to have:
map to:

…which should be simple enough. I found it helpful to write a simple test script test.php:

<?php echo "You've arrived at test.php
"; echo $title; ?>

Adding the following to .htaccess then maps http://mydomain.com/ to http://mydomain.com/test.php:

RewriteEngine on
RewriteRule ^$ test.php [L]

Check this works for you!

These rules map http://mydomain.com/article to http://mydomain.com/test.php?title=article:

RewriteEngine on
RewriteRule ^(.*)$ test.php?title=$1 [L,QSA]

Try this with various test strings. The QSA (query string append) flag appears to be necessary, else test.php reports its own name only. Which doesn’t quite square with my reading of the Apache mod_rewrite docs, but I’ll admit to being mostly baffled by them:
(still worth reading, though I wish there were more simple examples of RewriteRule!)

Note that remapping index.php appears to fail, that is:

RewriteRule ^(.*)$ index.php?title=$1 [L,QSA]

…simply doesn’t, for me. You’ll need to change the name of your index script, and map to that instead.


At this point, I had pretty much what I needed for MediaWiki. My install is in the served root directory (not a subdir); I renamed index.php to wiki.php. .htaccess is then:

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/(stylesheets|images|skins)/
RewriteCond %{REQUEST_URI} !^/(redirect|texvc|index).php
RewriteCond %{REQUEST_URI} !^/error/(40(1|3|4)|500).html
RewriteCond %{REQUEST_URI} !^/favicon.ico
RewriteCond %{REQUEST_URI} !^/robots.txt

RewriteRule ^$ wiki.php?title=Main_Page [L]
RewriteRule ^(.*)$ wiki.php?title=$1 [L,QSA]

This specifically excludes /stylesheets, /images, /skins etc from the rewrite match conditions (taken from http://meta.wikimedia.org/wiki/Rewrite_Rules)

The first RewriteRule simply masks the default page query (Main_Page) from the root page, tidying up the site home; the next rule maps everything else to the neat version.

Changes to LocalSettings.php I then modified from

$IP = “/path/to/your/served/directory”;

$wgSitename = “MyWikiSite.com”; // modify,
$wgServer = “http://mywikisite.com”; // obviously!
$wgScriptPath = “”;
$wgScript = “wiki.php”;
$wgRedirectScript = “/redirect.php”;

If using PHP as a CGI module, use the ugly URLs

So says the original LocalSettings, but we’re being bold, so:

$wgArticlePath = “/$1”; // enabled

$wgArticlePath = “$wgScript?title=$1”; // switched off

$wgStyleSheetPath = ‘/stylesheets’;
$wgStylePath = “/stylesheets”;
$wgStyleDirectory = “$IP/stylesheets”;

$wgUploadPath = “/images”;
$wgUploadDirectory = “$IP/images”;

I think that’s all I did - and I’m not even sure all the above is necessary, but it seems to work. But while I’m here; I wanted to enable maths rendering via texvc (ie. LaTeX/TeX fragment rendering). texvc failed to compile cleanly for me, but there’s a precompiled binary here:
…which seems to work fine if you unzip it and bung it in /maths, then turn it on in LocalSettings.php:

$wgUseTeX = true;
$wgMathPath = “{$wgUploadPath}/math”;
$wgMathDirectory = “{$wgUploadDirectory}/math”;
$wgTmpDirectory = “{$wgUploadDirectory}/tmp”;

You’ll need to make the render and temp directories in your upload path.

Good luck, and happy mod_rewriting!


Reply to own post - bad form, I know. Sorry.

For some reason, the above worked last night, but this morning the RewriteCond exceptions weren’t kicking in (?!). I moved the first RewriteRule above the RewriteCond lines, and now it seems to work. I’m not going to even think about why, I’m just going to consider myself lucky.