Manual:Short URL/Apache Rewrite rules

From MediaWiki.org

Jump to: navigation, search

Contents

[edit] Apache Rewrite method

Apache's mod-rewrite can be used to change illegally formatted "friendly" URL requests into legitimate requests directed at MediaWiki's index.php page.

[edit] Basics

Rewriting a URL consists of two parts, specifying conditional patterns to check for, and a transformation rule to apply to the URL if the condition is true. Take note of your DocumentRoot. Which is /home/johndoe/public_html in this example. Add to /home/johndoe/public_html/.htaccess or to httpd.conf:

httpd.conf method .htaccess method
<Directory /home/johndoe/public_html>
  # enable the rewriter
  RewriteEngine On
  # anything under /wiki is treated as an article title
  RewriteRule ^/wiki/(.+)$ mediawiki/index.php?title=$1 [PT,L,QSA] 
</Directory>
# enable the rewriter
RewriteEngine on
# anything under /wiki is treated as an article title
RewriteRule ^/wiki/(.+)$ mediawiki/index.php?title=$1 [PT,L,QSA] 

As you can see, the contents of the Directory block in httpd.conf are identical to .htaccess.

[edit] Adding rewrite rules

Usually additional rewrite rules are needed before a wiki will be fully functional or will work well with other web services you might be running. In the table below we list some common examples taken from real wikis that you may also need to use, depending on your individual needs.

If you want... add to httpd.conf or .htaccess
Skins and images to work RewriteRule ^(images|skins)/ - [L]
FCKeditor to work as well RewriteRule ^(images|skins|fckeditor)/ - [L]
Anything that ends in some common file extensions to work RewriteRule \.(php|html|gif|jpg|png|css|js)$ - [L]
Anything with a dot/period in it to work RewriteRule \. - [L]
Anything that looks like a directory path to work RewriteRule ^[^:]*[./] - [L]
Any files that exist to work RewriteCond %{REQUEST_FILENAME} !-f
Any directories that exist to work RewriteCond %{REQUEST_FILENAME} !-d
A redirect from yourdomain.example.com/wiki/ to Main_Page RewriteRule ^/wiki/$ mediawiki/index.php?title=Main_Page [L,QSA]
A redirect from yourdomain.example.com to Main_Page RewriteRule ^/*$ mediawiki/index.php?title=Main_Page [L,QSA]
Anything else under /wiki/ treated as articles of your wiki RewriteRule ^/wiki/(.+)$ mediawiki/index.php?title=$1 [L,QSA]
Anything else treated as articles of your wiki RewriteRule ^(.+)$ mediawiki/index.php?title=$1 [L,QSA]

For example:

 RewriteEngine On
 RewriteRule ^/(images|skins)/ - [L] # so skins and images work
 RewriteRule ^/*$ mediawiki/index.php?title=Main_Page [L,QSA]
 RewriteRule ^(.+)$ mediawiki/index.php?title=$1 [PT,L,QSA]

Remember after you edit httpd.conf to restart Apache.

[edit] Edit LocalSettings.php

Edit your LocalSettings.php as follows:

$wgScriptPath       = "/wiki";                         # default value
$wgScript           = "${wgScriptPath}/index.php";     # default value
$wgRedirectScript   = "${wgScriptPath}/redirect.php";  # default value
 
# This should be the only line you may have to edit in most cases.
# Use $wgArticlePath = "/$1"; if you want example.com/Page_Name instead
# of example.com/wiki/Page_Name.
$wgArticlePath      = "${wgScriptPath}/$1";

Or if you're using MediaWiki version 1.6.8:

$wgArticlePath      = "$wgScript/$1";

[edit] Notes

  • The above method works after changing the "mediawiki/index.php?title=$1" to "mediawiki/index.php/$1" in the .htaccess/httpd.conf rewrites part when using 'pretty' URLS.
  • Getting 404 errors? logout and login to update the cookie. Also, are you sure you have the Apache Rewrite engine enabled? You can enable the Apache2 rewrite engine module as follows (instructions are for Ubuntu 6.06.1): Note you can also type sudo a2enmod rewrite simply.
    • # cd /etc/apache2/mods-enabled/
    • # ln -s ../mods-available/rewrite.load .
  • for url with colon like "http://www.mediawiki.org/wiki/Manual:Short_URL" to be working use the record below in .htaccess: RewriteRule ^wiki/(.*:.*)$ wiki/index.php?title=$1 [L]
Personal tools