Manual:Short URL/Ampersand solution with root access

From MediaWiki.org

Jump to: navigation, search

Contents

[edit] A successful fix

[edit] Disclaimer

This worked for us on a w:DreamHost server (don't know the Apache version). It may or may not work for you (but I hope it does... good luck!).
Update: The same site was moved to a CentOS (basically RedHat) server running Apache2 and it worked fine on there as well.

[edit] The Fix

One successful implementation (according to testing so far...) on the MediaWiki 1.6.6 install at LyricWiki.org used the following .htaccess content to fix the ampersand problem and allow periods in page names.

Also works on MediaWiki 1.11.0 with apache 1.3.34 .htaccess

RewriteEngine on

RewriteRule ^[^:]*\.(php|src|jpg|jpeg|png|gif|bmp|css|js|inc|phtml|pl|ico|html|shtml)$ - [L,NC]
RewriteRule ^index.php?title - [L]
RewriteRule ^(.*)\&(.*)$ $1\%26$2
RewriteRule ^(.+)$ /index.php?title=$1 [L,QSA]

[edit] Demonstration
http://www.lyricwiki.org/Mind.in.a.box goes to http://www.lyricwiki.org/index.php?title=Mind.in.a.box
http://www.lyricwiki.org/Belle_&_Sebastian goes to http://www.lyricwiki.org/index.php?title=Belle_%26_Sebastian

[edit] Explanation
  • The first rule stops normal files (as opposed to Wiki pages) from listening to the rewrites. The [L] means that this is the last rule for those files to listen to and kicks them out. The [NC] means that the rule is Not Case sensitive.
  • The second rule makes sure that pages with index.php?title already in the url don't bother with the rewrite rules.
  • The third rule maps and ampersands to the correct version of the url with %26 substituted in. If this rule were not there, the re-write would not even accept your redirect if you had %26 in the original (pre-re-written) url because it would try to escape the %26 into something else.
  • The last line is the basic rewrite.

[edit] The Fix (slightly different solution)

I wanted this fix only for my wiki files, and so I modified it slightly.

This works on MediaWiki 1.12.0rc1 with Apache 2 .htaccess

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)\&(.*)$ $1\%26$2
RewriteRule ^(.+)$ index.php?title=$1 [L,QSA]

[edit] Explanation
  • The three conditions stop the rewrite for files, directorys and symbolic links. This is done for every file - not only for some files as the fix above does it.
  • The first rule maps and ampersands to the correct version as above.
  • The last line is the basic rewrite. (This can be prepended by the three conditions as well.)

[edit] Another option

The above solution only handles a single ampersand in the article title. My solution modifies the wiki code instead of htaccess and it extracts the title from the $_SERVER variable. Tested and working on 1.6.7. By Barrylb 19:35, 17 July 2006 (UTC)

Modify includes/WebRequest.php - function WebRequest() - put the following after global $wgUsePathInfo;:

  global $wgArticlePath;
  if (strpos($_SERVER['SCRIPT_NAME'], 'index.php') === false) {
    $articlePathPart =  str_replace('$1','',$wgArticlePath);
    $_GET['title'] = $_REQUEST['title'] = str_replace($articlePathPart, '', $_SERVER['SCRIPT_NAME']); 
  }
Personal tools