Eliminating index.php from the url: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
Revert to revision 577773 dated 2007-05-05 15:17:54 by 195.113.65.10 using popups
m Reverted changes by 2803:9800:988D:761B:7493:BF6F:E836:FF61 (talk) to last version by Johannnes89
Tag: Rollback
 
(17 intermediate revisions by 15 users not shown)
Line 1: Line 1:
{{MovedToMediaWiki|Manual:Short URL}}
'''The most current information on this subject is in the [http://www.mediawiki.org/wiki/Manual:Short_URL MediaWiki Manual on mediawiki.org].'''

The page describes the steps needed to '''eliminate index.php from the URL'''. The goal of this article to change viewing URLs to no longer include "index.php?title=", meanwhile edit pages and will still use "index.php?title=".

==Overview==
By default, URLs in MediaWiki will appear like this:
'''<nowiki>http://mywiki.site.tld/wiki/index.php?title=Article_name</nowiki>'''

This article deals with changing the URL to appear like this:
'''<nowiki>http://mywiki.site.tld/wiki/Article_name</nowiki>'''

However, editing pages under a changed system will still use URLs like this:
'''<nowiki>http://mywiki.site.tld/w/index.php?title=Article_name&action=edit</nowiki>'''

In LocalSettings.php, several lines of code are used to determine the make-up of the URL:
:<code>$wgScriptPath = "/w";</code>
:<code>$wgScript = "$wgScriptPath/index.php";</code>
:<code>$wgArticlePath = "$wgScript?title=$1";</code>

<code>$wgScriptPath</code> determines the root wiki directory. In the case directly above, all of the MediaWiki files are in a folder named "w". <code>$wgScript</code> references <code>$wgScriptPath</code> and tells your wiki where index.php can be found. In this case, index.php can be found directly inside the "w" folder. <code>$wgArticlePath</code> tells MediaWiki the path to the articles. <code>$wgArticlePath</code> references <code>$wgScript</code>, which references <code>$wgScriptPath</code> telling MediaWiki that in this case, article URLs are in /w/index.php?title=Article_name.

Depending on your individual system configuration, some of the methods listed may not be possible.

:''See also: [[Using a very short URL]] for instructions to eliminate even the "wiki" part of the URL.''
:''See also: [[MediaWiki User's Guide]].''

==Using aliases in httpd.conf==
This is the prefered method from a performance point of view, but requires access to edit httpd.conf. '''Most shared hosting systems do not allow changes to httpd.conf.''' The method below 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 = "" in LocalSettings.php.

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.''' (For example: do not try to rewrite "<tt>/wiki/Article</tt>" to "<tt>/wiki/index.php?title=Article</tt>).

3. Set up the following alias in your Apache httpd.conf. It can be in a <code>&lt;VirtualHost&gt;</code> section, or in "Aliases" section, or in your general site config. In this alias, the prefix <code>/filesystem/path/to/my/site</code> represents the path you installed to &mdash; the directory where MediaWiki's <code>index.php</code> 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

For example:

Alias /wiki "c:/apache/www/w/index.php"
Alias /index.php "c:/apache/www/w/index.php"

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

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 <code>[http://httpd.apache.org/docs-2.0/mod/core.html AcceptPathInfo]</code>. 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 can be used instead of edits to httpd.conf. .htaccess files allow the ability to command the Apache server when you're unable to edit the httpd.conf files. Using RewriteRules in a .htaccess file, you can tell the server to display URLs that don't include "index.php?title=". However, this method does require slightly more work for the server. Also, in order to use this method, '''''the MediaWiki install must be placed in the "w" directory, not in any other folder, including a folder named "wiki".''''' If you install MediaWiki into a folder name "wiki", you can simply rename the "wiki" folder to "w" and change your LocalSettings.php from <code>$wgScriptPath = "/wiki";</code> to <code>$wgScriptPath = "/w";</code>.

# In LocalSettings.php, change <code>$wgArticlePath = "$wgScript?title=$1";</code> to <code>$wgArticlePath = "/wiki/$1";</code>
# Create, or alter, a .htaccess file with the following content in the directory for www.mysite.com

You'll add two lines of code, the first enables the RewriteEngine, and the second tells the server to redirect URLs from /w/index.php?title=Article_name to /wiki/Article_name.

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

To tell the Apache server to redirect from www.mysite.com/ to www.mysite.com/wiki/Main_Page, you can add:
RewriteRule ^$ /wiki/Main_Page [R]

===Moving MediaWiki to another directory===
If you didn't follow the instructions and installed MediaWiki in the directory for www.mysite.com/wiki, then you will need to move it to another directory, such as "w" and then you will need to change <code>$wgScriptPath = "/wiki";</code> to <code>$wgScriptPath = "/w";</code> in LocalSettings.php

==Troubleshooting==
===Purging cache===
If you notice that your changes to <code>$wgArticlePath</code> 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.

Go to mysite.com/wiki/Main_Page?action=purge to force MediaWiki to regenerate the cached links.

Also you can:
# execute the MySQL query "DELETE FROM objectcache;", or
# set <tt>$wgCacheEpoch</tt> to the current date.

===Other issues===
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 i18n]] Localize/ customize things like the 'fromwikipedia' string
* [[MediaWiki User's Guide]]
* [[URL rewrite in IIS]]
See [[Talk:Eliminating index.php from the url|the talk page]] for more information on setting up non root rewrite rules.

Next page: [[PHP config]] >

[[Category:MediaWiki User's Guide]]

Latest revision as of 10:53, 22 January 2024