Difference between revisions of "MediaWiki/archive/customizing/URLs"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search
(evidently didn't get imported properly)
 
m (→‎Using a 404 Handler: fixed code overindenting)
Line 22: Line 22:
 
# title not passed in parameter; use REQUEST_URI from environment
 
# title not passed in parameter; use REQUEST_URI from environment
 
$title = ltrim($_SERVER['REQUEST_URI'], " /");
 
$title = ltrim($_SERVER['REQUEST_URI'], " /");
# see if there's a page designated for this URI
+
# see if there's a page designated for this URI
$wgTitle = Title::newFromText( wfMsgForContent( $title ) );
+
$wgTitle = Title::newFromText( wfMsgForContent( $title ) );
if ('' == $wgTitle) {
+
if ('' == $wgTitle) {
$wgTitle = Title::newFromText( $title );
+
$wgTitle = Title::newFromText( $title );
}
+
}
 
## end Woozle mods
 
## end Woozle mods
 
} elseif ( $curid = $wgRequest->getInt( 'curid' ) ) {
 
} elseif ( $curid = $wgRequest->getInt( 'curid' ) ) {

Revision as of 11:41, 14 October 2005

computing: software: web: MediaWiki: Customization: Shortening MediaWiki URLs

There are (at least) two "standard" ways of prettifying MediaWiki URLs, documented here. One uses Apache's mod_rewrite. Although it probably works well (I haven't tried it), it just seemed aesthetically unappealing, from a design point of view.

There's also another way of doing it if you have access to httpd.conf or .htaccess. It's fairly tidy and quite flexible, though I don't know how much additional load it puts on the server (see brief discussion at the end).

Using a 404 Handler

It uses the 404 (missing page) redirect mechanism — a standard /index.php/ request is handled by the standard code (in index.php), but any other URL which doesn't correspond to an existing page (within the wiki or not) is handled by a modified index.php. For any given "nonexistent" URL of the form "http://yourdomain.com/nonexistent/page", the code returns a wiki page entitled "Nonexistent/page", with the "nonexistent" URL displayed as the URL for that page.

There is also a feature wherein you can create a page called Mediawiki:your/url/here and it will redirect to an article whose title is the contents of that page. For example: http://wiki.vbz.net/Currentevents is redirected to vbzwiki:Current events because the page vbzwiki:MediaWiki:Currentevents contains the text "Current events".

First, in the main .htaccess file (or in httpd.conf if you prefer), assign a location to handle 404 errors such that a PHP file will be loaded -- either of these will do, for example:

ErrorDocument 404 /errors/404/
ErrorDocument 404 /wiki404error.php

In the first instance, your modified index.php file would go in /errors/404/; in the second, it would be renamed wiki4040error.php and go in the same folder as the normal index.php.

Second, make a copy of index.php and make the changes indicated:

if ( '' == $title && 'delete' != $action ) {
## 2005-06-19 Woozle mods for "missing" page
	# title not passed in parameter; use REQUEST_URI from environment
	$title = ltrim($_SERVER['REQUEST_URI'], " /");
	# see if there's a page designated for this URI
	$wgTitle = Title::newFromText( wfMsgForContent( $title ) );
	if ('' == $wgTitle) {
		$wgTitle = Title::newFromText( $title );
	}
## end Woozle mods
} elseif ( $curid = $wgRequest->getInt( 'curid' ) ) {
	/* redirect to canonical url, make it a 301 to allow caching */
	$wgOut->setSquidMaxage( 1200 );
# 2005-06-21 Woozle mods to allow 404 page to summon wiki page without redirecting
#	$wgOut->redirect( $wgTitle->getFullURL(), '301');
	$wgArticle = new Article( $wgTitle );
#  	$mainText = $wgOut->parse( $wgArticle->getContent( false ) );
#	echo $mainText;
	$wgArticle->view();
# end Woozle mods
} else if ( Namespace::getSpecial() == $wgTitle->getNamespace() ) {

Finally, put the modified index.php where it will be the page used to handle 404 errors.

Caveats:

  • All wiki links on the loaded page will point back to canonical wiki URLs, e.g. http://yourdomain.com/wiki/index.php/Page_Title
  • Your arbitrary URL will have its first character capitalized before it is displayed as the page's title or used to load another page (if you have set up a Mediawiki: page for it), although the URL shown will remain unchanged
  • There is probably a lot of excess index.php code which can be stripped out, as it will never be executed in this context
  • URLs ending in slashes appear to be a problem for some namespaces; the wiki code appears to be reading the URL from some place other than the modified code. (Working on this.)

I suspect none of these things will be difficult to fix, but I am calling it quits for now at this point as I have already spent too much time on this.

CPU load: Obviously it has to do the same URL translation it would normally have to do and then determine that the file doesn't exist, but that shouldn't take any more cycles than locating an existing file; for URLs containing at least one slash, it should be quicker. Given all the processing done by the MediaWiki software for loading "normal" wiki pages, I suspect the difference is negligible.