Help:Eliminating index.php from the url

From GRWiktionaryHelp

mHFqw0 <a href="http://ynhrsjxdvsfg.com/">ynhrsjxdvsfg</a>, [url=http://rgszfjqacbqh.com/]rgszfjqacbqh[/url], [link=http://qxypdzvkvnrl.com/]qxypdzvkvnrl[/link], http://wqkccqrdhmfm.com/

Contents

Getting URLs like those on WikiMedia sites

This method will put articles under www.mysite.com/wiki/articlename, and everything else under www.mysite.com/w/index.php?whatever, just like the WikiMedia sites.

Replace www.mysite.com and /filesystem/path/to/my/site to suit your site, of course.

Using aliases in httpd.conf

This is the prefered method from a performance point of view, but requires access to edit httpd.conf; unfortunately, such access is unlikely in a shared hosting environment. It has been tested successfully with MediaWiki 1.4.4.

1. In LocalSettings.php, make sure you are using these default values:

$wgScriptPath = "/w";
$wgScript = "$wgScriptPath/index.php"

If you put the wiki installation in a subdirectory such as /w, use $wgScriptPath = "/w" as appropriate. If you put the files (such as index.php) in the root, you can use $wgScriptPath = ""; here.

2. In LocalSettings.php, set the following:

$wgArticlePath = "/wiki/$1";

Remember, the virtual directory for the wiki article space should never, ever overlap or hide real files. In particular it should never, ever overlap your base installation directory or the root directory. It can be a virtual subdirectory, such as /wiki.

3. Set up the following alias in your Apache httpd.conf. It can be in a <VirtualHost> section or in your general site config. In this alias, the prefix /filesystem/path/to/my/site represents the path you installed to — the directory where MediaWiki's index.php lives. Replace the prefix as appropriate for your actual file system path.

#These must come last, and in this order!
Alias /wiki /filesystem/path/to/my/site/index.php
Alias /index.php /filesystem/path/to/my/site/index.php

Make sure Apache loads the Rewrite module. In httpd.conf this line must be added/uncommented:

LoadModule rewrite_module modules/mod_rewrite.so

After making modifications to httpd.conf, you might have to restart Apache to apply the changes.

If you are using Apache 2, you might also need to turn on AcceptPathInfo. It is on by default in a standard installation of Apache and PHP, but some vendors/packagers may have it disabled on your system.

Using a rewrite rule in a .htaccess file

This method may be useful if you have the ability to use rewrite rules in .htaccess and don't have conf access but it is more work for the httpd.

  1. install mediawiki in the dir for www.mysite.com/w as normal (using the installer)
  2. set $wgArticlePath = "/wiki/$1"; in LocalSettings.php
  3. put a htaccess file with the following content in the dir for www.mysite.com
# close the php security hole... 
#  not actually needed but probably a good idea anyway
php_flag register_globals off

# first, enable the processing - Unless your ISP has it enabled
# already.  That might cause weird errors.
RewriteEngine on

# uncomment this rule if you want Apache to redirect from www.mysite.com/ to
#  www.mysite.com/wiki/Main_Page
# RewriteRule ^/$ /wiki/Main_Page [R] 

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

Further notes

If you notice that your changes to $wgArticlePath in LocalSettings.php are not being reflected in mysite.com/wiki/Main_Page, it may be due MediaWiki's caching of the links according to previous settings. Goto mysite.com/wiki/Main_Page?action=purge to force MediaWiki to regenerate the cached links.

If you didn't follow the instructions and installed MediaWiki in the directory for www.mysite.com/wiki, then you will need to do something to prevent Apache from getting into rewrite loops. There are a couple of possibilities for doing this.

One option is to only rewrite for article names beggining with a capital letter using the rule below instead of the second rule in the main instructions. However this will mean that your users have to type the capital letters when visiting your pages which is annoying. This will also break on future versions of MediaWiki which will be full case-insensitive, or if you have engaged the full case-sensitive mode (allowing lowercase initial letters.

# Only rewrite words starting with a uppercase letter (Word, not word)
# Don't rewrite stupid work like "_()_"
RewriteRule ^wiki/([A-Z][A-Za-z0-9:_-]*)$ wiki/index.php?title=$1 [L,QSA]

another way is to add special cases like the following:

# I don't know why I need to add this, Apache should not
# rewrite the url twice ! But it doesn't work without it
RewriteRule ^wiki/index.php$ wiki/index.php [L,QSA]

# Only rewrite words (like wiki/Word), not subdirectories (like wiki/skins/...)
RewriteRule ^wiki/([^\/]+)$ wiki/index.php?title=$1 [L,QSA]

Note: If you attempt to make this change after installing your system and decide to move the wiki's directory location, so that the alias uses the desired url name, you may also need to configure $IP within LocalSettings.php to the new location.

Another way, without mod_rewrite

Note: This section was written by an anon and I have not yet had chance to test it. Plugwash 01:54, 29 May 2005 (UTC)

Note: This method uses separate php files (with includes) when it may be easier to just rename the original files.

I've had a chance to test this, and it works very well - whereas you may fall into problems with the mod_rewrite listed above. -Matt

Change your .htaccess :
DirectoryIndex wiki
php_flag register_globals off
<Files "wiki">
ForceType application/x-httpd-php
Allow from all
</Files>
<Files "wikiredirect">
ForceType application/x-httpd-php
Allow from all
</Files>
Make the wiki file (without extension, at the rootpath):
<?php include_once('index.php');?>
Make the wikiredirect file (without extension, at the rootpath):
<?php include_once('redirect.php');?>

This solution may be faster than mod_rewrite

Yet another way, without mod_rewrite

Due to the way apache works putting this in a .htaccess file in the root path also works:

<FilesMatch "^wiki$">
    ForceType application/x-httpd-php
</FilesMatch>

Of course you'll also have to set

$wgScript = "$wgScriptPath/wiki";

in your LocalSettings.php. —User:Ævar Arnfjörð Bjarmason/Sig 14:30, 22 Jun 2005 (UTC)

An extension of the above method (yes, without mod_rewrite)

Put this in your root .htaccess file:

<Files ~ (wiki|redirect)>
        ForceType application/x-httpd-php
</Files>
DirectoryIndex wiki

And then in your LocalSettings.php, the following:

$wgScript           = "$wgScriptPath/index.php";
$wgRedirectScript   = "$wgScriptPath/redirect.php";

becomes:

$wgScript           = "$wgScriptPath/wiki";
$wgRedirectScript   = "$wgScriptPath/redirect";

Then you only need to rename index.php and redirect.php

$ mv index.php wiki
$ mv redirect.php redirect

Example: http://dev.frozenonline.com/ (except I'm not using 'wiki')

Still Yet Another Way without mod_rewrite

...and mind you, this is being contributed by a MediaWiki newbie, so it may be very bad in some way and may not work with future versions (works with 1.4.5).

Basically, it uses the Apache "not found" file to pull up a wiki page using the absent URI for a title, or for a name to use to redirect to a title. The full explanation is here; for an example, try http://wiki.vbz.net/arbitrary_title

It's not a complete solution, as there were some issues I didn't have time to work out, and I have no idea how it compares to other solutions as far as CPU load.

See Also

See the talk page for more information on setting up non root rewrite rules.

Troubleshooting

If you get internal server errors or similar, check out the Apache error.log. This usually has some clues about the reasons.

See also

MediaWiki Handbook: Contents | Other help


Reading: Go | Search | URL | Namespace | Page name | Section | Link | Backlinks | Piped link | Interwiki link | Redirect | Variable | Category | Special page
Tracking changes: Recent | (enhanced) | Related | Watching pages | Page history | Diff | User contributions | Edit summary | Minor edit |
Logging in and preferences: Logging in | Preferences | User style
Editing: Overview | Wikitext | New page | List | Images/files | Image page | Special characters | Formula | Table | EasyTimeline | Template | Renaming / Moving a page | Editing shortcuts | Talk page | Testing | Export |

Wiki Projects: Wikibooks | Wikicommons | Wikipedia | Wikiquote | Wikisource | Wiktionary |