Manual:Short URL/Ampersand solution
Contents |
[edit] Simple solution for Apache 2.2.7+
Add the ‘B’ flag to your RewriteRule that maps the path component to a title parameter. For example, change
RewriteRule ^/wiki/(.*)$ /w/index.php?title=$1 [PT,L,QSA]
to
RewriteRule ^/wiki/(.*)$ /w/index.php?title=$1 [PT,L,QSA,B]
[edit] A successful fix (older Apache)
[edit] Disclaimer
This worked for us on a 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
RewriteRule ^(.*)\&(.*)$ $1\%26$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
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']);
}
[edit] And yet another option
This single line in your .htaccess will replace ALL ampersands:
RewriteRule ^([^\&]*)\&(.*) $1\%26$2 [N]
[N] ensures a greedy regexp (repeats for all matches) It's compatible with, at least, Apache 2.0.59.
[edit] Rename offending articles
Another option, if you have only a small number of articles with the ampersand problem, is to rename the articles. To rename one article:
- Run the script
maintenance/moveBatch.phpsupplied with MediaWiki. Supply the old name and new name, separated by a pipe symbol, on standard input. The page is moved. - Visit the page under its new name and click "What links here" in the toolbox menu.
- In the results, locate the bad title (which now redirects to the new title), click it, then click the delete tab.
Pages created with a forbidden character cannot be easily moved or deleted. See Help:Page naming.
[edit] Solution without server access
An admin can move an article without needing server access, but it's somewhat involved.
This has been tested for "&", but presumably works with other forbidden characters. Steps:
- Export the page(s) with Special:Export - this will work in spite of the "&". Save as xml file.
- Open the xml file in a text editor.
- Look for the <title> tag
- Edit the title, removing or replacing all forbidden characters, e.g. "&" with "and". Save the file.
- Import with Special:Import. View the page to confirm that it has been imported successfully.
- Delete the original page with the bad title. This is tricky:
- Find the form of the delete page url in your wiki. Copy this url, e.g. for the page "foo bar" it might be http://www.appropedia.org/index.php?title=Foo_bar&action=delete
- Replace the title with the bad page title, that you want to delete
- Replace spaces in the title section with underscores: _
- Replace the ampersand - & - with %26. So to delete a page called "A & B" in the example above, you would use the url http://www.appropedia.org/index.php?title=A_%26_B&action=delete
- Replace other forbidden characters - this hasn't been test here, but it's likely that you can replace them by %nn where nn is the hex code given in this table of printable characters for that character.
- Now you have the url, enter it in your browser, hit enter, and confirm the deletion.
[edit] Redirect to view
If you just need to view one of these articles quickly, create a redirect to it, then hit the redirect. It will render the desired article.
