特殊:Badtitle/NS100:EnablingUseOfApacheHtaccessFiles:修订间差异
小无编辑摘要 |
小无编辑摘要 |
||
第2行: | 第2行: | ||
{{Languages|UbuntuHelp:EnablingUseOfApacheHtaccessFiles}} | {{Languages|UbuntuHelp:EnablingUseOfApacheHtaccessFiles}} | ||
This page was written based on Ubuntu 6.10 (Edgy), although the same may apply to other releases. | This page was written based on Ubuntu 6.10 (Edgy), although the same may apply to other releases. | ||
By default, Ubuntu's Apache will ignore the directives in your < | By default, Ubuntu's Apache will ignore the directives in your <pre><nowiki>.htaccess</nowiki></pre> files. | ||
=== When (not) to | === When (not) to u<pre><nowiki>.htaccess</nowiki></pre>les === | ||
According to [http://httpd.apache.org/docs/2.0/howto/htaccess.html#when Apache.org's Apache Tutorial], | According to [http://httpd.apache.org/docs/2.0/howto/htaccess.html#when Apache.org's Apache Tutorial], | ||
''"In general, you should never | ''"In general, you should never u<pre><nowiki>.htaccess</nowiki></pre>les unless you don't have access to the main server configuration file. There is, for example, a prevailing misconception that user authentication should always be done <pre><nowiki>.htaccess</nowiki></pre>les. This is simply not the case. You can put user authentication configurations in the main server configuration, and this is, in fact, the preferred way to do things."'' | ||
<pre><nowiki>.htaccess</nowiki></pre>les should be used in a case where the content providers need to make configuration changes to the server on a per-directory basis, but do not have root access on the server system. In the event that the server administrator is not willing to make frequent configuration changes, it might be desirable to permit individual users to make these changes <pre><nowiki>.htaccess</nowiki></pre>les for themselves."'' | |||
On Ed/X/Ubuntu 6.06 and Ubuntu Edgy Eft, the ''"main server configuration file"'' is < | On Ed/X/Ubuntu 6.06 and Ubuntu Edgy Eft, the ''"main server configuration file"'' is <pre><nowiki> /etc/apache2/apache2.conf </nowiki></pre>. | ||
=== OK, I know it is not recommended -- how do I do it anyway? === | === OK, I know it is not recommended -- how do I do it anyway? === | ||
To make < | To make <pre><nowiki>.htaccess</nowiki></pre> files work as expected, you need to edit <pre><nowiki>/etc/apache2/sites-available/default</nowiki></pre>. Look for a section that looks like this: | ||
<pre><nowiki> <Directory /var/www/> | <pre><nowiki> <Directory /var/www/> | ||
Options Indexes FollowSymLinks MultiViews | Options Indexes FollowSymLinks MultiViews | ||
AllowOverride None | AllowOverride None | ||
Order allow,deny | Order allow,deny | ||
allow from all | allow from all | ||
# Uncomment this directive is you want to see apache2's | # Uncomment this directive is you want to see apache2's | ||
# default start page (in /apache2-default) when you go to / | # default start page (in /apache2-default) when you go to / | ||
#RedirectMatch ^/$ /apache2-default/ | #RedirectMatch ^/$ /apache2-default/ | ||
</Directory> | </Directory> | ||
</nowiki></pre> | </nowiki></pre> | ||
You need to modify the line containing < | You need to modify the line containing <pre><nowiki>AllowOverride None</nowiki></pre> to read <pre><nowiki>AllowOverride All</nowiki></pre>. This tells Apache that it's okay to allow <code><nowiki>.htaccess</nowiki></code> files to over-ride previous directives. You must ''reload'' Apache before this change will have an effect: | ||
<pre><nowiki>sudo /etc/init.d/apache2 reload | <pre><nowiki>sudo /etc/init.d/apache2 reload | ||
</nowiki></pre> | </nowiki></pre> | ||
See http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride for more info on < | See http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride for more info on <pre><nowiki>AllowOverride</nowiki></pre>. | ||
=== Password-Protect a Directory With .htaccess === | === Password-Protect a Directory With .htaccess === | ||
'''Warning: On at least some versions of Ubuntu, <code><nowiki>.htaccess</nowiki></code> files will not work by default. See [[UbuntuHelp:EnablingUseOfApacheHtaccessFiles|EnablingUseOfApacheHtaccessFiles]] for help on enabling them.''' | '''Warning: On at least some versions of Ubuntu, <code><nowiki>.htaccess</nowiki></code> files will not work by default. See [[UbuntuHelp:EnablingUseOfApacheHtaccessFiles|EnablingUseOfApacheHtaccessFiles]] for help on enabling them.''' | ||
第33行: | 第33行: | ||
require valid-user | require valid-user | ||
</nowiki></pre> | </nowiki></pre> | ||
instead of < | instead of <pre><nowiki>valid-user</nowiki></pre>, you can also add the users you want directly | ||
If you want to password protect just a single file in a folder add the following lines to the <code><nowiki>.htaccess</nowiki></code> file: | If you want to password protect just a single file in a folder add the following lines to the <code><nowiki>.htaccess</nowiki></code> file: | ||
<pre><nowiki> | <pre><nowiki> | ||
<Files "mypage.html"> | <Files "mypage.html"> | ||
Require valid-user | Require valid-user | ||
</Files> | </Files> | ||
</nowiki></pre> | </nowiki></pre> | ||
Then create the file < | Then create the file <pre><nowiki>/your/path/.htpasswd</nowiki></pre> which contains the users that are allowed to login and their passwords. We do that with the <pre><nowiki>htpasswd</nowiki></pre> command: | ||
<pre><nowiki> | <pre><nowiki> | ||
htpasswd -c /path/to/your/.htpasswd user1 | htpasswd -c /path/to/your/.htpasswd user1 | ||
</nowiki></pre> | </nowiki></pre> | ||
The < | The <pre><nowiki>-c</nowiki></pre> flag is used only when you are creating a new file. After the first time, you will omit the <pre><nowiki>-c</nowiki></pre> flag, when you are adding new users to an already-existing password file. Otherwise you will overwrite the file!! | ||
Nevertheless, you should store the file in as secure a location as possible, with whatever minimum permissions on the file so that the web server itself can read the file. | Nevertheless, you should store the file in as secure a location as possible, with whatever minimum permissions on the file so that the web server itself can read the file. | ||
Finally we need to add the following lines to < | Finally we need to add the following lines to <pre><nowiki>/etc/apache2/apache2.conf</nowiki></pre>: | ||
<pre><nowiki> | <pre><nowiki> | ||
<Directory /your/path> | <Directory /your/path> | ||
第52行: | 第52行: | ||
</Directory> | </Directory> | ||
</nowiki></pre> | </nowiki></pre> | ||
You have to adjust < | You have to adjust <pre><nowiki>/your/path/.htpasswd</nowiki></pre> | ||
Restart your webserver: | Restart your webserver: | ||
<pre><nowiki> | <pre><nowiki> | ||
第58行: | 第58行: | ||
</nowiki></pre> | </nowiki></pre> | ||
==== Troubleshooting ==== | ==== Troubleshooting ==== | ||
If you can't access your stuff and the dialog keeps popping up, check that you entered the username and password correctly. If it still doesn't work, check the path to your < | If you can't access your stuff and the dialog keeps popping up, check that you entered the username and password correctly. If it still doesn't work, check the path to your <pre><nowiki>.htpasswd</nowiki></pre> and make sure the path specified in the <pre><nowiki>AuthUserFile directive</nowiki></pre> is correct. Also make sure that both the <pre><nowiki>.htpasswd</nowiki></pre> and <code><nowiki>.htaccess</nowiki></code> files are readable by the web server user | ||
< | <pre><nowiki>chmod 644</nowiki></pre> should do the trick! | ||
==== Example ==== | ==== Example ==== | ||
Here is an example on how to prevent users from access the directory, password-protect a specific file and allow userse to view a specific file: | Here is an example on how to prevent users from access the directory, password-protect a specific file and allow userse to view a specific file: | ||
第68行: | 第68行: | ||
Order Allow,Deny | Order Allow,Deny | ||
<Files myfile1.html> | <Files myfile1.html> | ||
Order Allow,Deny | Order Allow,Deny | ||
require valid-user | require valid-user | ||
</Files> | </Files> | ||
<Files myfile2.html> | <Files myfile2.html> | ||
Order Deny,Allow | Order Deny,Allow | ||
</Files> | </Files> | ||
</nowiki></pre> | </nowiki></pre> | ||
== Redirect requests using .htaccess a<pre><nowiki>mod_rewrite</nowiki></pre> | |||
Make sure Apache .htaccess is enabled and the Apache module <code><nowiki>mod_rewrite</nowiki></code> is enabled and then you can redirect requests using RewriteRules | Make sure Apache .htaccess is enabled and the Apache module <code><nowiki>mod_rewrite</nowiki></code> is enabled and then you can redirect requests using RewriteRules | ||
<pre><nowiki> | <pre><nowiki> |
2007年12月6日 (四) 10:13的版本
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/EnablingUseOfApacheHtaccessFiles }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/af | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|Afrikaans| [[::EnablingUseOfApacheHtaccessFiles/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/ar | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|العربية| [[::EnablingUseOfApacheHtaccessFiles/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/az | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|azərbaycanca| [[::EnablingUseOfApacheHtaccessFiles/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/bcc | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|جهلسری بلوچی| [[::EnablingUseOfApacheHtaccessFiles/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/bg | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|български| [[::EnablingUseOfApacheHtaccessFiles/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/br | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|brezhoneg| [[::EnablingUseOfApacheHtaccessFiles/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/ca | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|català| [[::EnablingUseOfApacheHtaccessFiles/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/cs | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|čeština| [[::EnablingUseOfApacheHtaccessFiles/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/de | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|Deutsch| [[::EnablingUseOfApacheHtaccessFiles/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/el | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|Ελληνικά| [[::EnablingUseOfApacheHtaccessFiles/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/es | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|español| [[::EnablingUseOfApacheHtaccessFiles/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/fa | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|فارسی| [[::EnablingUseOfApacheHtaccessFiles/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/fi | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|suomi| [[::EnablingUseOfApacheHtaccessFiles/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/fr | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|français| [[::EnablingUseOfApacheHtaccessFiles/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/gu | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|ગુજરાતી| [[::EnablingUseOfApacheHtaccessFiles/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/he | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|עברית| [[::EnablingUseOfApacheHtaccessFiles/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/hu | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|magyar| [[::EnablingUseOfApacheHtaccessFiles/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/id | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|Bahasa Indonesia| [[::EnablingUseOfApacheHtaccessFiles/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/it | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|italiano| [[::EnablingUseOfApacheHtaccessFiles/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/ja | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|日本語| [[::EnablingUseOfApacheHtaccessFiles/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/ko | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|한국어| [[::EnablingUseOfApacheHtaccessFiles/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/ksh | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|Ripoarisch| [[::EnablingUseOfApacheHtaccessFiles/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/mr | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|मराठी| [[::EnablingUseOfApacheHtaccessFiles/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/ms | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|Bahasa Melayu| [[::EnablingUseOfApacheHtaccessFiles/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/nl | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|Nederlands| [[::EnablingUseOfApacheHtaccessFiles/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/no | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|norsk| [[::EnablingUseOfApacheHtaccessFiles/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/oc | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|occitan| [[::EnablingUseOfApacheHtaccessFiles/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/pl | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|polski| [[::EnablingUseOfApacheHtaccessFiles/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/pt | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|português| [[::EnablingUseOfApacheHtaccessFiles/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/ro | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|română| [[::EnablingUseOfApacheHtaccessFiles/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/ru | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|русский| [[::EnablingUseOfApacheHtaccessFiles/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/si | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|සිංහල| [[::EnablingUseOfApacheHtaccessFiles/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/sq | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|shqip| [[::EnablingUseOfApacheHtaccessFiles/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/sr | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|српски / srpski| [[::EnablingUseOfApacheHtaccessFiles/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/sv | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|svenska| [[::EnablingUseOfApacheHtaccessFiles/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/th | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|ไทย| [[::EnablingUseOfApacheHtaccessFiles/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/tr | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|Türkçe| [[::EnablingUseOfApacheHtaccessFiles/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/vi | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|Tiếng Việt| [[::EnablingUseOfApacheHtaccessFiles/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/yue | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|粵語| [[::EnablingUseOfApacheHtaccessFiles/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/zh | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|中文| [[::EnablingUseOfApacheHtaccessFiles/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/zh-hans | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|中文(简体)| [[::EnablingUseOfApacheHtaccessFiles/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles | UbuntuHelp:EnablingUseOfApacheHtaccessFiles | {{#if: | :}}EnablingUseOfApacheHtaccessFiles}}/zh-hant | • {{#if: UbuntuHelp:EnablingUseOfApacheHtaccessFiles|中文(繁體)| [[::EnablingUseOfApacheHtaccessFiles/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:EnablingUseOfApacheHtaccessFiles|:EnablingUseOfApacheHtaccessFiles|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :EnablingUseOfApacheHtaccessFiles/zh | | {{#ifexist: EnablingUseOfApacheHtaccessFiles/zh | | {{#ifeq: {{#titleparts:EnablingUseOfApacheHtaccessFiles|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:EnablingUseOfApacheHtaccessFiles|1|-1|}} | zh | | }}
This page was written based on Ubuntu 6.10 (Edgy), although the same may apply to other releases.
By default, Ubuntu's Apache will ignore the directives in your
.htaccess
files.
When (not) to u.htaccess
les
According to Apache.org's Apache Tutorial,
"In general, you should never u
.htaccess
les unless you don't have access to the main server configuration file. There is, for example, a prevailing misconception that user authentication should always be done
.htaccess
les. This is simply not the case. You can put user authentication configurations in the main server configuration, and this is, in fact, the preferred way to do things."
.htaccess
les should be used in a case where the content providers need to make configuration changes to the server on a per-directory basis, but do not have root access on the server system. In the event that the server administrator is not willing to make frequent configuration changes, it might be desirable to permit individual users to make these changes
.htaccess
les for themselves." On Ed/X/Ubuntu 6.06 and Ubuntu Edgy Eft, the "main server configuration file" is
/etc/apache2/apache2.conf
.
OK, I know it is not recommended -- how do I do it anyway?
To make
.htaccess
files work as expected, you need to edit
/etc/apache2/sites-available/default
. Look for a section that looks like this:
<Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all # Uncomment this directive is you want to see apache2's # default start page (in /apache2-default) when you go to / #RedirectMatch ^/$ /apache2-default/ </Directory>
You need to modify the line containing
AllowOverride None
to read
AllowOverride All
. This tells Apache that it's okay to allow .htaccess
files to over-ride previous directives. You must reload Apache before this change will have an effect:
sudo /etc/init.d/apache2 reload
See http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride for more info on
AllowOverride
.
Password-Protect a Directory With .htaccess
Warning: On at least some versions of Ubuntu, .htaccess
files will not work by default. See EnablingUseOfApacheHtaccessFiles for help on enabling them.
Create a file called .htaccess
in the directory you want to password-protect with the follwing content:
AuthUserFile /your/path/.htpasswd AuthName "Authorization Required" AuthType Basic require valid-user
instead of
valid-user
, you can also add the users you want directly
If you want to password protect just a single file in a folder add the following lines to the .htaccess
file:
<Files "mypage.html"> Require valid-user </Files>
Then create the file
/your/path/.htpasswd
which contains the users that are allowed to login and their passwords. We do that with the
htpasswd
command:
htpasswd -c /path/to/your/.htpasswd user1
The
-c
flag is used only when you are creating a new file. After the first time, you will omit the
-c
flag, when you are adding new users to an already-existing password file. Otherwise you will overwrite the file!!
Nevertheless, you should store the file in as secure a location as possible, with whatever minimum permissions on the file so that the web server itself can read the file.
Finally we need to add the following lines to
/etc/apache2/apache2.conf
:
<Directory /your/path> AllowOverride All </Directory>
You have to adjust
/your/path/.htpasswd
Restart your webserver:
sudo /etc/init.d/apache2 restart
Troubleshooting
If you can't access your stuff and the dialog keeps popping up, check that you entered the username and password correctly. If it still doesn't work, check the path to your
.htpasswd
and make sure the path specified in the
AuthUserFile directive
is correct. Also make sure that both the
.htpasswd
and .htaccess
files are readable by the web server user
chmod 644
should do the trick!
Example
Here is an example on how to prevent users from access the directory, password-protect a specific file and allow userse to view a specific file:
AuthUserFile /your/path/.htpasswd AuthName "Authorization Required" AuthType Basic Order Allow,Deny <Files myfile1.html> Order Allow,Deny require valid-user </Files> <Files myfile2.html> Order Deny,Allow </Files>
== Redirect requests using .htaccess a
mod_rewrite
Make sure Apache .htaccess is enabled and the Apache module mod_rewrite
is enabled and then you can redirect requests using RewriteRules
RewriteRule ^x.x$ y.y