keepcalculator

06 Numbers and Powers

Enable Short URLs in MediaWiki: Apache & Nginx

Card No. 06·80242 · Updated · Standard formula, shown below

Short URLs make MediaWiki pages cleaner and easier to share by removing index.php?title= from the address. This guide walks through the exact configuration for both Apache and Nginx, including the required LocalSettings.php changes and common pitfalls.

What You’ll Need

  • Administrative access to your MediaWiki installation (files and database).
  • Access to your web server configuration files (Apache httpd.conf or .htaccess, Nginx nginx.conf or site config).
  • SSH or file manager access to edit files.
  • MediaWiki already installed and working.

Step-by-Step

1. Choose Your URL Structure

Decide on a pattern. Common choices are /wiki/Page_Title (recommended) or /wiki/Page_Title with a different script path. This guide uses /wiki/ as the article path and keeps the default script path at /w/.

2. Edit LocalSettings.php

Open LocalSettings.php in your MediaWiki root and add or modify these lines (near the end, before any closing ?>):

$wgScriptPath = "/w";
$wgArticlePath = "/wiki/$1";
$wgUsePathInfo = true;

If you don’t have a /w directory, you can set $wgScriptPath = "" but that is not recommended for security. Save the file.

3. Configure Apache

If you use Apache with mod_rewrite enabled, you have two options:

Using .htaccess (shared hosting): Create or edit .htaccess in your web root (where LocalSettings.php resides) and add:

RewriteEngine On
RewriteRule ^/?wiki(/.*)?$ /w/index.php [L]
RewriteRule ^/?w/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?w/(.*)$ /w/index.php?title=$1 [L,QSA]

Using VirtualHost (dedicated server): In your <VirtualHost> block, add:

<Directory /var/www/html>
    AllowOverride All
</Directory>

Then place the same .htaccess rules in the directory, or put them directly in the VirtualHost (without RewriteCond for files/dirs if you want to skip those checks).

4. Configure Nginx

In your Nginx server block, add a location block for the article path. Place this before any generic location / block:

location /wiki/ {
    rewrite ^/wiki/(.*)$ /w/index.php?title=$1 last;
}

location /w/ {
    try_files $uri $uri/ /w/index.php?$query_string;
}

If you use a different script path, adjust the rewrite accordingly. For example, if $wgScriptPath is empty, use rewrite ^/wiki/(.*)$ /index.php?title=$1 last;.

5. Clear Caches

After making changes, clear the MediaWiki caches:

  • Run php maintenance/rebuildLocalisationCache.php from the MediaWiki root (optional but recommended).
  • If you have object caching (e.g., Memcached, Redis), flush it.
  • Restart your web server: sudo systemctl restart apache2 or sudo systemctl restart nginx.

6. Test

Visit a page like http://yourdomain.com/wiki/Main_Page. It should load correctly. Also check that old URLs like http://yourdomain.com/w/index.php?title=Main_Page still work (they should redirect to the short form).

Common Mistakes

  • Forgetting to enable mod_rewrite – Apache will ignore .htaccess rules if AllowOverride is set to None or if the module is disabled.
  • Incorrect path in rewrite rule – If your MediaWiki is in a subdirectory (e.g., /var/www/mediawiki), the rewrite target must include that subdirectory (e.g., /mediawiki/w/index.php).
  • Not setting $wgUsePathInfo – On some setups, leaving it false will cause “page not found” errors.
  • Nginx location order – If a generic location / block is defined before location /wiki/, it may intercept the request.
  • Editing the wrong .htaccess – The .htaccess must be in the same directory as LocalSettings.php, not the web root if they differ.

Tips

  • Use /wiki/ as the article path and keep the script path as /w/ – this is the standard MediaWiki convention and avoids conflicts with existing directories.
  • Back up your LocalSettings.php and server config before editing.
  • Test with a private browser window or incognito to avoid cached redirects.
  • If you have a caching layer (like Varnish or Cloudflare), purge the cache for your domain after making changes.
  • For Nginx, consider using try_files instead of rewrite for better performance: location /wiki/ { try_files $uri $uri/ /w/index.php?title=$1; } – but note this requires the $1 to be captured in a named location or using a regex location. Simpler is the rewrite method shown.

Troubleshooting

Page not found (404) when accessing /wiki/Page

  • Check that the rewrite rule is active: for Apache, run sudo a2enmod rewrite and restart.
  • Verify the rewrite target path is correct – try accessing /w/index.php?title=Main_Page directly to see if it works.
  • Look at the server error log for rewrite messages.

Redirect loop or “Too many redirects”

  • This often happens when the article path and script path overlap. Ensure $wgArticlePath is different from $wgScriptPath (e.g., /wiki/ vs /w/).
  • Clear browser cache and cookies.

Old URLs stop working

  • Add a redirect from index.php?title= to the short form. In Apache: RewriteRule ^/w/index\.php\?title=(.*)$ /wiki/$1 [R=301,L] (adjust for your paths). In Nginx: location ~ ^/w/index\.php { if ($arg_title) { return 301 /wiki/$arg_title; } } – but test carefully.
  • Alternatively, you can leave old URLs working without redirect; they will still function.

CSS/JS not loading

  • This is usually a path issue. Ensure $wgScriptPath is set correctly and that the /w/ directory is accessible.
  • Check the browser console for 404 errors on resource files.

Frequently Asked Questions

Do I need to change the database? No, short URLs are purely a web server and configuration change. The database remains unchanged.

Can I use a different path like /articles/ instead of /wiki/? Yes, just set $wgArticlePath = "/articles/$1"; and adjust the rewrite rules accordingly.

Will this break existing bookmarks? No, old index.php?title= URLs will still work (they will be rewritten or served directly). You may want to add redirects for SEO, but it’s not required.

What if I’m using IIS instead of Apache/Nginx? This guide covers Apache and Nginx only. For IIS, you’d need URL Rewrite module rules, which are different.

Why does my wiki show “Action not recognized” after enabling short URLs? This usually happens when the rewrite rule is too broad and captures non-article URLs. Ensure your rewrite only matches the article path.

Is it safe to use a subdirectory like /wiki/ if I already have a directory named /wiki? Yes, as long as the rewrite rule is configured to handle it. MediaWiki will take precedence over the physical directory if the rewrite is correct.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →