Search code examples
apachemod-rewritemediawiki

Can I get rid of the '/wiki/' part of the url?


Can I get rid of the '/wiki/' part of the url?

I created my own MediaWiki as a hobby project. I had to remove the url of my website in this post because the forum marked it as spam.

Initially, my urls looked like this:

https://[website]/Main_Page
https://[website]/edit/Main_Page
https://[website]/history/Main_Page

etc.

My .htaccess had this:

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d

RewriteRule ^(.*)$ w/index.php?title=$1 [L,QSA]
RewriteRule ^$ w/index.php [L,QSA]

My LocalSettings.php had this:

$wgScriptPath = "/w";
$wgArticlePath = "/$1";
$wgScriptExtension = ".php";
$wgUsePathInfo = true;

$wgServer = "[website]";

$wgResourceBasePath = $wgScriptPath;

$actions = array( 'view', 'edit', 'watch', 'unwatch', 'delete', 'revert', 'rollback', 'protect', 'unprotect', 'markpatrolled', 'render', 'submit', 'history', 'purge', 'info' );

foreach ( $actions as $action ) {
    $wgActionPaths[$action] = "/$action/$1";
}

The problem occurred after I installed the Flagged Revisions extension. FlaggedRevs uses rest.php. When FlaggedRevs tried to access this API, the server gave back a 404 on this url: https://[website]/w/rest.php/flaggedrevs/internal/review/Main_Page

What happened was: Apache and MediaWiki tried to literally load a wiki article named "W/rest.php/flaggedrevs/internal/review/Main Page". In other words, instead of loading the REST API, FlaggedRevs tried loading an non-existing page, which of course caused it to fail.

I attempted a number of things, and also asked for help at mediawiki.org. Then, I tried inserting "/wiki/" into the url. Surprisingly, this solved the problem!

My .htaccess became:

(...)
RewriteRule ^wiki/(.\*)$ w/index.php?title=$1 \[L,QSA\]
RewriteRule ^wiki$ w/index.php \[L,QSA\]
RewriteRule ^$ w/index.php \[L,QSA\]

My LocalSettings.php became:

$wgArticlePath = "/wiki/$1";
(...)
$wgActionPaths\[$action\] = "/wiki/$action/$1";

So, technically, this fixed the issue. But now I'm stuck with this stupid "wiki" bit in the url of my wiki (https://[website]/wiki/Main_Page).

My question is: is there a way to get rid of it? Can I put a rule in the .htaccess to help Apache find rest.php, instead of a page titled "W/rest.php/*"?


Solution

  • Fixed! Thanks to @student91. The solution was "RewriteCond %{REQUEST_URI} !^/w/rest.php".

    Full .htaccess:

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