Home > Software engineering >  Mediawiki redirect from old URL path starting with /w/ to new path without it
Mediawiki redirect from old URL path starting with /w/ to new path without it

Time:11-27

I have old URL path for all pages

example.com/w/Page_title

Now I changed it to

example.com/Page_title

using Short URL manual for Apache

And the question is: How to make 301 redirects from old path for users that coming back using bookmarks?

my LocalSettings.php

$wgForceHTTPS = true;

$wgScriptPath = "/wiki";
$wgArticlePath = "/$1";

and mod_rewrite:

RewriteEngine On
# Short URL for wiki pages
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [L]
# Redirect Main Page to /
RewriteRule ^/*$ /index.php?title=Main_Page [L,QSA]
RewriteRule .*\.ico$ - [L]

CodePudding user response:

You need to add a RewriteRule to do the redirect before the other rules:

RewriteRule \/?w\/(.*) /$1 [R=301,L]

This matches any URL that starts with /w/ and redirects to remove it.

  • \/? - An optional starting slash so this rule will work in different contexts (Apache conf and .htaccess)
  • w\/ - The w/ directory
  • (.*) - Everything after the w/ in a capturing group that becomes $1 in the target
  • /$1 - Where to redirect to
  • R=301 Make this a permanent (301) redirect
  • L - Make this the last rewrite rule so that the other rules don't get processed when it matches.

In the end it should look like this with your other rules:

RewriteEngine On
# Redirect old URLs that start with /w/
RewriteRule \/?w\/(.*) /$1 [R=301,L]
# Short URL for wiki pages
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [L]
# Redirect Main Page to /
RewriteRule ^/*$ /index.php?title=Main_Page [L,QSA]
RewriteRule .*\.ico$ - [L]

  • Related