Manual:Short URL/Page title -- PHP as a CGI module, no root access

From MediaWiki.org

Jump to: navigation, search

I don't remember where I got this from, but it just works like a charm for me.

Please update this article if you find any bugs to it.

Contents

[edit] In LocalSettings.php

Remove or comment out (with a # or / at the beginning of the line) any existing definition of $wgArticlePath, for example:

#$wgArticlePath      = "$wgScript/$1";
#$wgArticlePath      = "$wgScript?title=$1";

Instead, add the following line, best somewhere near the definition of $wgScriptPath:

$wgArticlePath      = "/$1";

Add the following line so edit, logout, etc. links work:

$wgUsePathInfo     = false;

[edit] In .htaccess

Copy and paste the following content into the .htaccess file, in the root directory of your website (often in the folder public_html).

If the .htaccess file does not exist - create it in public_html.

RewriteEngine On
RewriteRule ^[^:]*\. - [L]
RewriteRule ^[^:]*\/ - [L]
RewriteRule ^/*$ /wiki/index.php?title=Main_Page [L,QSA]
RewriteRule ^(.+)$ /wiki/index.php?title=$1 [L,QSA]

Please note: once you install mediawiki in root folder (not /wiki subfolder) .htaccess should be:

RewriteEngine On
RewriteRule ^[^:]*\. - [L]
RewriteRule ^[^:]*\/ - [L]
RewriteRule ^/*$ /index.php?title=Main_Page [L,QSA]
RewriteRule ^(.+)$ /index.php?title=$1 [L,QSA]

Explanation:

RewriteEngine On
# anything line which contains a dot (.) without a colon (:) should be left alone
RewriteRule ^[^:]*\. - [L]
# anything line which contains a slash (/\) without a colon (:) should be left alone
RewriteRule ^[^:]*\/ - [L]
# redirect to main page
RewriteRule ^/*$ /wiki/index.php?title=Main_Page [L,QSA]
# anything else is to be treated as a title
RewriteRule ^(.+)$ /wiki/index.php?title=$1 [L,QSA]
  • This assumes all the files are located in the /wiki/ folder. If the your folder has another name, change the above /wiki/ folder to the name of your folder. Example: /wiki/ to /Example/,
  • If MediaWiki is located in the root directory of your website, remove "/wiki"

If you are using .php5 extensions (your urls appear with a .php5 instead of the normal .php) then change the Rewriterules to:

RewriteEngine On
# anything line which contains a dot (.) without a colon (:) should be left alone
RewriteRule ^[^:]*\. - [L]
# anything line which contains a slash (/\) without a colon (:) should be left alone
RewriteRule ^[^:]*\/ - [L]
# redirect to main page
RewriteRule ^/*$ /wiki/index.php5?title=Main_Page [L,QSA]
# anything else is to be treated as a title
RewriteRule ^(.+)$ /wiki/index.php5?title=$1 [L,QSA]

[edit] Alternate option

This article or section may be too technical for a general audience.
Please help improve this article by providing more context and better explanations of technical details to make it more accessible, without removing technical details.

I've found the following works better if you have subpages on NS_MAIN. Replace the first two RewriteRules with:

 RewriteCond         %{REQUEST_FILENAME}  -f [OR]
 RewriteCond         %{REQUEST_FILENAME}  -d [OR]
 RewriteCond         %{REQUEST_FILENAME}  -l
 RewriteRule  ^(.+)  /path/to/web/directory/$1  [L]

This causes Apache to serve a file if it is available as a either a file, directory, or symbolic link. Make sure you replace /path/to/web/directory/ to the path that contains your .htaccess file. On a dedicated server this might be /var/www/. On a shared host it might be /home/username/sites/sitename/ or something similar.


[edit] Alternate option II

This article or section may be too technical for a general audience.
Please help improve this article by providing more context and better explanations of technical details to make it more accessible, without removing technical details.

Both versions above have flaws.

The version below doesn't have these two disadvantages:

RewriteEngine on

RewriteCond         %{REQUEST_URI} !index.php
RewriteCond         %{REQUEST_FILENAME}  -f [OR]
RewriteCond         %{REQUEST_FILENAME}  -d [OR]
RewriteCond         %{REQUEST_FILENAME}  -l
RewriteRule  ^(.+)  /path/to/web/directory/$1  [L]

# redirect to main page
RewriteRule ^/*$ /index.php?title=Main_Page [L,QSA]

# anything else is to be treated as a title
RewriteCond         %{REQUEST_URI} !index.php
RewriteRule ^(.+)$ /index.php?title=$1 [L,QSA]

[edit] Alternate option III

All non-existing URLs are treated as Titles.


Options -Indexes

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

[edit] Tips

This article or section may be too technical for a general audience.
Please help improve this article by providing more context and better explanations of technical details to make it more accessible, without removing technical details.

If you want to reorder things and put the files in a /wiki/ folder, you don't have to reinstall the whole thing. Just make sure you update $wgScriptPath within LocalSettings.php to reflect the name of the directory where you put the files, and of course update the .htaccess file as instructed above.

[edit] Known issues

  • Problem: Any template links will have the /wiki/ in the web page address, breaking the webpage link and causing a 404 page not found error.
    • Partial Solution:
      Add Subst: in all of the web page templates.
      Example: {{Fox}} becomes {{Subst:Fox}} The subst: command causes the code of the template to appear directly on the page. The problem is that any future changes to the template will not be changed on those pages you added the subst: too.
  • Problem: Clicking on "log out" takes the wiki to the page "index.php", rather than logging out. The index.php is now gone in the URL.
  • Problem: After installing the Main Option everything works fine except links to pages in the footer, which trigger a 404 error because the footer is not calling the short URLs. After installing Alternate Option 2, the site functions properly for a few page refreshes, then all site formatting disappears.
    • Solution: Suggestions are welcome, particularly to access links to Privacy Policy, etc. in the footer.
  • Problem: When using the first option and going to my website without /wiki/index.php/Main_Page it links me to the Main_Page. After clicking on any link the URL shows up as /wiki/index.php/TITLE instead of only showing the title. (www.mysite.com/Main_Page and clicking on a link will go to www.mysite.com/Example_Title).
    • Solution: Please fill in a solution if you have solved this issue!


  • Everything works fine except of wiki.com/FAQ link. (see: larp.pl)
Personal tools