Current time: 04-19-2014, 09:08 PM Hello There, Guest! (LoginRegister)

Post Reply 
.htaccess, mod_rewrite, PHP-CGI, and all that jazz
09-26-2004, 05:21 PM
Post: #1
.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:
<http://mydomain.com/Article>
map to:
<http://mydomain.com/index.php?title=Article>

...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<br />";
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:
<http://httpd.apache.org/docs/mod/mod_rewrite.html>
(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.

MediaWiki
=========

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
<http://wise-nano.org/w/Programming_notes>:

$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:
<http://download.wikimedia.org/texvc-linux-x86-20040107.tar.gz>
...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!
Visit this user's website Find all posts by this user
Quote this message in a reply
09-27-2004, 02:04 AM
Post: #2
.htaccess, mod_rewrite, PHP-CGI, and all that jazz
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: