more probably coming
Apache > mod_rewrite > MediaWiki >
-
Terms: mod_rewrite, Rewriting, URL rewriting, Friendly URLs, pretty URLs, etc.
-
2005-08-15 -- Redirection apparently hasn't been working for weeks.
- This means that all references to the old URLs won't really work. I tried fixing this, but mod_rewrite is a nightmare.
-
I had an old todo that said that rewriting was breaking Google sitemaps.
A Simple rewrite solution for MediaWiki ∞
In the root ∞
php_flag register_globals off RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?title=$1 [L,QSA] # one person adds: # RewriteRule ^(.*)$ /index.php?title=$1 [PT,L,QSA]
$wgArticlePath = "/$1";
In a subdirectory ∞
Our example uses the "wiki
" directory.
php_flag register_globals off RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /<b>wiki</b>/index.php?title=$1 [L,QSA]
$wgArticlePath = "/<b>wiki</b>/$1";
My mod_rewrite rules ∞
My mod_rewrite rules ∞
This wasn't updated since I moved my wiki from /
to the subdirectory /wiki
. It was also never properly documented.
[[Special:Version]]
∞
[[Special:Version]]
reports:
MediaWiki https://www.mediawiki.org/wiki/MediaWiki <span class="footnote_referrer" ><a><sup id="footnote_plugin_tooltip_47787_1" class="footnote_plugin_tooltip_text" onclick="footnote_moveToAnchor_47787('footnote_plugin_reference_47787_1');" >[ 1 ]</sup ></a><span id="footnote_plugin_tooltip_text_47787_1" class="footnote_tooltip" > was [[archive:http://wikipedia.sf.net/|]] </span ></span><script type="text/javascript"> jQuery('#footnote_plugin_tooltip_47787_1').tooltip({ tip: '#footnote_plugin_tooltip_text_47787_1', tipClass: 'footnote_tooltip', effect: 'fade', predelay: 0, fadeInSpeed: 200, delay: 400, fadeOutSpeed: 200, position: 'top center', relative: true, offset: [1, 0], });</script>): 1.3.11 PHP https://www.php.net/ ): 5.0.3 (apache2handler) MySQL https://www.mysql.com/ ): 4.1.10
LocalSettings.php
∞
I don't use localhost/wiki/Main_Page
or the like.. just localhost/Main_Page
. This means that I changed references to /wiki
to /
and also customised my $wgArticlePath
:
$wgArticlePath = "/$1";
httpd.conf
∞
Modify Apache's httpd.conf and uncomment the line which looks like:
LoadModule rewrite_module modules/mod_rewrite.so
Append the following to the bottom of your httpd.conf
:
The rules ∞
# Enable the rewriting feature RewriteEngine on # RewriteBase /wiki # Don't rewrite for official subdirectories and files: # I believe "skins" should be added for 1.4: RewriteCond %{REQUEST_URI} !^/(stylesheets|images)/ RewriteCond %{REQUEST_URI} !^/(redirect|texvc|index).php RewriteCond %{REQUEST_URI} !^/error/(40(1|3|4)|500).html RewriteCond %{REQUEST_URI} !^/favicon.ico RewriteCond %{REQUEST_URI} !^/robots.txt # Don't rewrite query strings, but do rewrite searches RewriteCond %{QUERY_STRING} ^$ [OR] %{REQUEST_URI} ^/Special:Search # Maybe subdirectories need this: #RewriteCond %{REQUEST_URI} ^/wiki$ # One of these would be the main rule: # This truncates question marks (%3F) RewriteRule ^(.*)$ /index.php$1 [L,QSA,NE] # Fails with ampersands? (not tested) #RewriteRule ^(.*)$ /index.php?title=$1 [L,QSA,NE]
Issues ∞
- You'd have to edit any templates which are hard-coded.
- I don't know how to have .htaccess available as a wiki page.
-
Does
mod_rewrite
support variables?- The installation directory.
Main_Page
-
Somehow these rules allow lowercase searches to go to an uppercase-titled page.
- i.e.
Main page
goes tohttp://localhost/Main_Page
- i.e.
-
"Edit" URLs are ugly
http://localhost/index.php?title=Main_Page&action=edit
-
Everything after a questionmark (
?
) is ignored.- Does MediaWiki even allow page titles with a questionmark?
Testing rewriting ∞
Copy-paste this into a page in MediaWiki.
{| |- ! Source ! Target ! Result |- | http://localhost/<br>{{SERVER}} | http://localhost/ | pass with<br />http://localhost/Main_Page |- | http://localhost/Main_Page<br>{{SERVER}}/Main_Page | http://localhost/Main_Page | pass |- | http://localhost/Testing<br>{{SERVER}}/Testing | http://localhost/Testing<br />(if it exists) | pass |- | http://localhost/index.php/Special:Search?search=Testing<br>{{SERVER}}/index.php/Special:Search?search=Testing | http://localhost/Testing<br />(if it exists) | pass |- | http://localhost/index.php/Special:Search?search=Special:Version<br>{{SERVER}}/index.php/Special:Search?search=Special:Version | http://localhost/Special:Version | pass |- | http://localhost/Special:Randompage<br>{{SERVER}}/Special:Randompage | http://localhost/<b>Page</b><br />(as appropriate) | pass |}
Rewrite rules, with MediaWiki in a subdirectory ∞
An update:
dev/.htaccess -> RewriteRule ^(.*)$ /dev/index.php?title=$1 [L,QSA] dev/LocalSettings.php -> dev/$wgArticlePath = "/dev/$1";
This has been tested and works!
# The RewriteCond ensures that next RewriteRule is only executed under certain conditions. # This is to help eliminate the chance for administrator screwups and might improve speed. # Rewrite www.example.org/wiki to www.example.org/wiki/Main_Page RewriteCond %{REQUEST_URI}/wiki$ ^/wiki/* RewriteRule ^/wiki/$ /wiki/index.php/Main_Page [QSA,NC,L] # Rewrite www.example.org/wiki/ to www.example.org/wiki/Main_Page RewriteCond %{REQUEST_URI}/wiki/$ ^/wiki/* RewriteRule ^/wiki/$ /wiki/index.php/Main_Page [QSA,NC,L] # Rewrite www.example.org/wiki/foo to www.example.org/wiki/index.php/foo but display as www.example.org/wiki/foo RewriteCond %{REQUEST_URI}/wiki/(.*)$ ^/wiki/* RewriteRule ^/wiki/(.*)$ /wiki/index.php/$1 [QSA,NC,L]
Testing ∞
Copy-paste this into a page in MediaWiki.
{| |- ! Source ! Target ! Current status |- | http://localhost/wiki/<br>{{SERVER}}/wiki/ | http://localhost/wiki/ | works |- | http://localhost/wiki (no trailing slash)<br>{{SERVER}}/wiki | http://localhost/wiki/ | works |- | http://localhost/wiki/Main_Page<br>{{SERVER}}/wiki/Main_Page | http://localhost/wiki/Main_Page | works |- | http://localhost/wiki/Testing<br>{{SERVER}}/wiki/Testing | http://localhost/wiki/Testing<br />(if it exists) | works |- | http://localhost/wiki/index.php/Special:Search?search=Testing<br>{{SERVER}}/wiki/index.php/Special:Search?search=Testing | http://localhost/wiki/Testing<br />(if it exists) | dies |- | http://localhost/wiki/index.php/Special:Search?search=Special:Version<br>{{SERVER}}/wiki/index.php/Special:Search?search=Special:Version | http://localhost/wiki/Special:Version | dies |- | http://localhost/wiki/Special:Randompage<br>{{SERVER}}/wiki/Special:Randompage | http://localhost/wiki/<b>Page</b><br />(as appropriate) | dies |}
2008-02-23 - MediaWiki 1.11.1 ∞
in the root ∞
/.htaccess
RewriteEngine On # Don't rewrite requests for files in MediaWiki subdirectories, # MediaWiki PHP files, HTTP error documents, favicon.ico, or robots.txt RewriteCond %{REQUEST_URI} !^/(stylesheets|images|skins)/ RewriteCond %{REQUEST_URI} !^/(redirect|texvc|index).php RewriteCond %{REQUEST_URI} !^/error/(40(1|3|4)|500).html RewriteCond %{REQUEST_URI} !^/favicon.ico RewriteCond %{REQUEST_URI} !^/robots.txt # Rewrite http://subdomain.example.com/article properly, this is the main rule RewriteRule ^(.*)$ /index.php/?title=$1 [L,QSA]
LocalSettings.php
# short urls $wgArticlePath = "/$1"; $wgUsePathInfo = false;
Installed in a subdirectory ∞
/.htaccess
php_flag register_globals off php_flag safe_mode off RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /dev/index.php?title=$1 [L,QSA]
LocalSettings.php
$wgArticlePath = "$wgScript?title=$1";
Learn if mod_rewrite is active ∞
Create /index.php
with:
<?php if (isset($_GET['section'])) echo "Rewriting works properly!<br>"; ?> <a href="./test_link">Click here</a>
/.htaccess
:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([\w]+)/?$ index.php?section=$1 [L,QSA]
Resources ∞
-
rewriting the edit and other URLs in MediaWiki
- Doesn't work for me, but it might be useful to investigate one year later.
-
Apache v1.3+ Module mod_rewrite URL Rewriting Engine [ 2 ] old, archive
- https://www.yourhtmlsource.com/sitemanagement/urlrewriting.html
- https://www.sitepoint.com/guide-url-rewriting/
- https://web.archive.org/web/20200115093244/http://www.workingwith.me.uk:80/articles/scripting/mod_rewrite
--
-
- I might still have this downloaded somewhere or other. [ 4 ] A note said it was
htaccesser-0.11.tar.gz
which is 17kB
- I might still have this downloaded somewhere or other. [ 4 ] A note said it was
Footnotes
^ 1 | was http://wikipedia.sf.net/ |
^ 2 | old, archive |
^ 3 | old, archive |
^ 4 | A note said it was htaccesser-0.11.tar.gz which is 17kB |
partially ported
I'll have to go through more things to determine what goes here and what should go in the main mod_rewrite topic.
MAAAAAYBE one day I might want to use this information. Maybe. Oh god, maybe.. that sounds like a self-threat.
ported