Difference between revisions of "Htaccess"
From Noah.org
Jump to navigationJump to searchLine 1: | Line 1: | ||
[[Category:Engineering]] | [[Category:Engineering]] | ||
== Protect access to files on a web server == | == Protect access to files on a web server == | ||
+ | |||
+ | This shows how to setup basic auth protection for files or directories. | ||
=== .htaccess === | === .htaccess === | ||
+ | |||
.htaccess for protecting individual files. | .htaccess for protecting individual files. | ||
<pre> | <pre> | ||
Line 27: | Line 30: | ||
=== .htpasswd === | === .htpasswd === | ||
+ | |||
Create an .htpasswd file for the username "staff": | Create an .htpasswd file for the username "staff": | ||
<pre> | <pre> | ||
Line 34: | Line 38: | ||
<pre> | <pre> | ||
staff:H.j5THn495T97 | staff:H.j5THn495T97 | ||
+ | </pre> | ||
+ | |||
+ | == mod_rewrite for MediaWiki friendly URLs == | ||
+ | |||
+ | This is a pain in the ass to get right. There are not a lot of pieces, but I had to keep fiddling with it to get it right. | ||
+ | |||
+ | Assuming you want the root directory to be /wiki (just like Wikipedia). For example: | ||
+ | https://www.noah.org/wiki/Htaccess | ||
+ | |||
+ | First move your wiki from wiki/ to w/. This is the <em>filesystem</em> path, not the URI. For example: | ||
+ | mv /var/www/htdocs/wiki /var/www/htdocs/w | ||
+ | |||
+ | === MediaWiki LocalSettings.php === | ||
+ | |||
+ | <pre> | ||
+ | $wgScriptPath = "/w"; | ||
+ | $wgArticlePath = "/wiki/$1"; | ||
+ | </pre> | ||
+ | |||
+ | === .htaccess (above the MediaWiki directory) == | ||
+ | |||
+ | <pre> | ||
+ | RewriteEngine on | ||
+ | |||
+ | # This rewrites URIs in the form /wiki/Article_Name to /w/index.php/Article_Name. | ||
+ | RewriteCond %{REQUEST_URI} !^/wiki/index.php.*$ [NC] | ||
+ | RewriteRule ^wiki/?(.*)$ /w/index.php/$1 [L] | ||
+ | |||
+ | # This takes old style links to articles in the form /wiki/index.php/Article_Name and | ||
+ | # redirects them to the new-style format /wiki/Article_Name. | ||
+ | RewriteCond %{REQUEST_URI} ^/wiki/index.php/.*$ [NC] | ||
+ | RewriteRule ^wiki/index.php/(.*)$ http://www.noah.org/wiki/$1 [R=301,NC,L] | ||
</pre> | </pre> |
Revision as of 06:45, 17 January 2008
Contents
Protect access to files on a web server
This shows how to setup basic auth protection for files or directories.
.htaccess
.htaccess for protecting individual files.
AuthName "Restricted Area" AuthType Basic AuthUserFile .htpasswd AuthGroupFile /dev/null <Files file_1.foo> require valid-user </Files> <Files file_2.foo> require valid-user </Files>
.htaccess for protecting the whole directory.
AuthName "Restricted Area" AuthType Basic AuthUserFile .htpasswd AuthGroupFile /dev/null require valid-user
.htpasswd
Create an .htpasswd file for the username "staff":
htpasswd -c .htpasswd user_name
It will look something like this:
staff:H.j5THn495T97
mod_rewrite for MediaWiki friendly URLs
This is a pain in the ass to get right. There are not a lot of pieces, but I had to keep fiddling with it to get it right.
Assuming you want the root directory to be /wiki (just like Wikipedia). For example:
https://www.noah.org/wiki/Htaccess
First move your wiki from wiki/ to w/. This is the filesystem path, not the URI. For example:
mv /var/www/htdocs/wiki /var/www/htdocs/w
MediaWiki LocalSettings.php
$wgScriptPath = "/w"; $wgArticlePath = "/wiki/$1";
= .htaccess (above the MediaWiki directory)
RewriteEngine on # This rewrites URIs in the form /wiki/Article_Name to /w/index.php/Article_Name. RewriteCond %{REQUEST_URI} !^/wiki/index.php.*$ [NC] RewriteRule ^wiki/?(.*)$ /w/index.php/$1 [L] # This takes old style links to articles in the form /wiki/index.php/Article_Name and # redirects them to the new-style format /wiki/Article_Name. RewriteCond %{REQUEST_URI} ^/wiki/index.php/.*$ [NC] RewriteRule ^wiki/index.php/(.*)$ http://www.noah.org/wiki/$1 [R=301,NC,L]