Can I make all links lowercase using MediaWiki and use hyphen instead of underscore as word separator?
Can I make in MediaWiki links lowercase and use 开发者_开发问答hyphen instead of underscore as word separator?
Sort of what WordPress or other wiki CMSes do.
Well there's a way to do this within the main namespace at least so the user edited pages have all-lowercase/dash-separated URLs.
You start by manually enforcing your pages to be all-lower case and dash-separated. The initial problem with this is that your internal links now have to have dashes in them, which eliminates the standard plain-language style of internal links and makes them almost unusable:
For examples of this see [[making-mediawiki-links-lowercase|making mediawiki links lowercase]].
Making a new wikilink is no longer adding a pair of double square brackets; the whole link text has to be rewritten (and optionally renamed with a pipe to be readable).
But there's a way to rewrite just the href on the links. First, make two changes to LocalSettings.php:
$wgCapitalLinks = False; # like mentioned above: make MW not capitalize the page titles
$wgHooks['LinkEnd'][] = "seoUrls"; # add a hook to rewrite the links
function seoUrls($dummy, Title $target, array $options, &$html, array &$attribs, &$ret) {
$attribs = str_replace("_", "-", $attribs);
return True;
}
This rewrites the href of internal links to use dashes and not underscores while leaving the rest of the anchor untouched.
Then use CSS to hide (or modify the template file to remove) the regular title on those pages with user content. The CSS will be something like:
.ns-0.action-view #firstHeading, /* main */
.ns-14.action-view #firstHeading /* categories */
{
display: none
}
You do want to leave the title for pages without any user-defined content, like the editing views and special pages. I found that only namespaces 0 (main) and 14 (categories) need to be hidden.
Lastly, instead of the Mediawiki-supplied title, give the page a title by adding an h1 to the top of the page's content itself.
You probably want to rewrite all URLs with underscores to be dashes as well:
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
Problems
- The non-existent page detection is not entirely accurate, because the pages it's searching for have spaces, not dashes. However, as long as you've eliminated all the old pages that have spaces in the title, this won't be a problem.
- Because the initial letter is now case-sensitive, the internal links require renames when they're the first word of the sentence.
- The table of contents and the inline edit section links will act differently; they're triggered now on every page due to the h1.
Advantages
- You can make the titles anything you want. I like to use category pages to have content, but don't like the title of "Category:Blah".
- You can also have the page title use mediawiki-illegal or non-ASCII characters without screwing with the URL.
It is possible to make MediaWiki use dashes instead of underscores to indicate whitespace. wikiHow does it. But it requires a number of edits to the source code, which is usually not advised. Also, there is no guarantee to be without bugs.
It looks like you can adjust the link case setting with this configuration value. I wasn't able to find anything regarding the word separator. Good luck.
To make a page name lowercase, use: Template:Lowercase. Add {{Lowercase}}
to any page to change how it is displayed.
Links can be used lowercase anyway - the very first letter of a link can be upper or lower case - it will always link to the correct page.
精彩评论