Manual:Short URL/Page title -- PHP as a CGI module, no root access
From MediaWiki.org
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]
or (1.13.3 version)
RewriteEngine On RewriteRule ^[^:]*\. - [L] RewriteRule ^[^:]*\/ - [L] RewriteRule ^/*$ /index.php/Main_Page [L,QSA] RewriteRule ^(.+)$ /index.php/=$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
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
Both versions above have flaws.
- In the first version ("In .htaccess") URLs with dots or slashes won't work (like http://server/Description_of_MediaWiki_3.0 or http://server/Using_/_editing) - user will get HTTP 404 error
- In the second version ("Alternate version") old URLs will not work - so if you previously had pages like http://server/index.php/Some_page, and now you have http://server/Some_page - the former will not work any more. No problem if you just started the site from scratch, but if there are links to the page already (search engines, links from other pages etc.), users will not be able to see the content.
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] Alternate option IV
This version fixes several of the problems encountered in other solutions, such as support for ampersands, periods, colons, and forward slashes.
The second line listing extensions needs every filetype that you wish to use in the wiki in the array.
RewriteEngine on
RewriteRule ^[^:]*\.(php|src|jpg|jpeg|png|gif|css|js|inc|swf|flv|phtml|pl|ico|html|shtml|zip|rar|svg)$ - [L,NC]
RewriteRule ^index.php?title - [L]
RewriteRule ^(.*)\&(.*)$ /index.php?title=$1\%26$2 [L,QSA]
RewriteCond %{REQUEST_URI} (//+)
RewriteRule ^(.*)/(.*)$ /index.php?title=$1%1$2 [L,QSA]
RewriteRule ^(.+)$ /index.php?title=$1 [L,QSA]
[edit] Alternate option V
Same as IV, but that didn't work for me. First I had to move the WIKISUBDIR back into the code. Then still errors from the websites base url like mydomain.org. So I added a specific Main_Page rule of this articles first steps back at the end:
RewriteEngine on
RewriteRule ^[^:]*\.(php|src|jpg|jpeg|png|gif|css|js|inc|swf|flv|phtml|pl|ico|html|shtml|zip|rar|svg)$ - [L,NC]
RewriteRule ^index.php?title - [L]
RewriteRule ^(.*)\&(.*)$ /WIKISUBDIR/index.php?title=$1\%26$2 [L,QSA]
RewriteCond %{REQUEST_URI} (//+)
RewriteRule ^(.*)/(.*)$ /WIKISUBDIR/index.php?title=$1%1$2 [L,QSA]
RewriteRule ^(.+)$ /WIKISUBDIR/index.php?title=$1 [L,QSA]
RewriteRule ^/*$ /WIKISUBDIR/index.php?title=Main_Page [L,QSA]
Now finally everything seems to work, as it was not with the other proposed options with my installation. Note that I'm just installing, so don't know about future page & other problems + that I'm not familiar with this program code, just experimenting many hours & more or less accidentally this worked. So someone more technically in this field, should look into my proposal & might fine tune more.
[edit] Tips
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.
- Partial Solution:
- Problem: Web page Logo does not appear in shortened web pages but still appears on normal
/wiki/index.php?title=pages.- Solution:
Use an absolute path to the image like so$wgLogo = "http://examplewiki.org/wiki/skins/common/images/wikiheader.png";
- Solution:
- 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.
- Solution:
Set the $wgUsePathInfo to false.
- Solution:
- 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!
- Problem: When you uses Windows and mod_rewrite including a ":"(colon)(e.g. Special:RecentChanges or other not-ns:0 pages), this method does not work (403 error).
- Cause: Windows uses a ":" for its drive mapping.
- Partial Solution: If you allow to use not-shortend URL for these page , modify getLocalURL function in Title.php. Shortend URLs are provided for only ns:0 pages.
} else { $url = str_replace( '$1', $dbkey, $wgArticlePath ); # THIS PROVIDES NOT-SHORTEND URL FOR NON-NS:0 PAGES if(strpos($dbkey, ':') != false) { $url = substr_replace( $url, "index.php?title=", 0, 1); } }
- Everything works fine except of wiki.com/FAQ link. (see: larp.pl)