{"id":1587,"date":"2013-03-28T09:42:14","date_gmt":"2013-03-28T09:42:14","guid":{"rendered":"http:\/\/blog.designed79.co.uk\/?p=1587"},"modified":"2013-03-28T09:42:14","modified_gmt":"2013-03-28T09:42:14","slug":"ubuntu-apache2-enable-htaccess","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=1587","title":{"rendered":"Ubuntu Apache2 enable .htaccess"},"content":{"rendered":"<div id=\"title\">\n<h1><a title=\"Click to do a full-text search for this title\" href=\"https:\/\/help.ubuntu.com\/community\/EnablingUseOfApacheHtaccessFiles?action=fullsearch&amp;value=linkto%3A%22EnablingUseOfApacheHtaccessFiles%22&amp;context=180\" rel=\"nofollow\">EnablingUseOfApacheHtaccessFiles<\/a><\/h1>\n<\/div>\n<div id=\"cwt-nav3\">\n<hr \/>\n<\/div>\n<div id=\"cwt-content\">\n<div dir=\"ltr\" id=\"page\" lang=\"en\">\n<div dir=\"ltr\" id=\"content\" lang=\"en\">\n<p><strong>WARNING<\/strong>: This page was written based on Ubuntu 10.04 (Lucid), although the same may apply to other releases. You are encouraged to also read:<\/p>\n<ol type=\"1\">\n<li>The related\u00a0<a href=\"https:\/\/help.ubuntu.com\/\">ubuntu server guide<\/a>\u00a0(Choose your distribution version &gt; &#8220;Ubuntu Server Guide&#8221; &gt; &#8220;Web servers&#8221;).<\/li>\n<li>The apache htaccess tutorial:\u00a0<a href=\"http:\/\/httpd.apache.org\/docs\/2.0\/howto\/htaccess.html\">http:\/\/httpd.apache.org\/docs\/2.0\/howto\/htaccess.html<\/a><\/li>\n<\/ol>\n<p>By\u00a0<strong>default<\/strong>, Ubuntu&#8217;s Apache will\u00a0<strong>ignore<\/strong>\u00a0the directives in your\u00a0<tt>.htaccess<\/tt>\u00a0files.<\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"When_.28not.29_to_use_.htaccess_files\">When (not) to use .htaccess files<\/h2>\n<p>According to\u00a0<a href=\"http:\/\/httpd.apache.org\/docs\/2.0\/howto\/htaccess.html#when\">Apache.org&#8217;s Apache Tutorial<\/a>,<\/p>\n<p><em>&#8220;In general, you should never use .htaccess files unless you don&#8217;t have access to the main server configuration file. There is, for example, a prevailing misconception that user authentication should always be done in .htaccess files. 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.&#8221;<\/em><\/p>\n<p><em>&#8220;.htaccess files 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 in .htaccess files for themselves.&#8221;<\/em><\/p>\n<p>On Ed\/X\/Ubuntu 6.06 and Ubuntu Edgy Eft, the\u00a0<em>&#8220;main server configuration file&#8221;<\/em>\u00a0is\u00a0<tt>\u00a0\/etc\/apache2\/apache2.conf\u00a0<\/tt>.<\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"OK.2C_I_know_it_is_not_recommended_--_how_do_I_do_it_anyway.3F\">OK, I know it is not recommended &#8212; how do I do it anyway?<\/h2>\n<p>To make\u00a0<tt>.htaccess<\/tt>\u00a0files work as expected, you need to edit this file:<\/p>\n<pre>\/etc\/apache2\/sites-available\/default<\/pre>\n<p>Look for a section that looks like this:<\/p>\n<pre>        &lt;Directory \/var\/www\/&gt;\r\n                Options Indexes FollowSymLinks MultiViews\r\n                AllowOverride None\r\n                Order allow,deny\r\n                allow from all\r\n                # Uncomment this directive is you want to see apache2's\r\n                # default start page (in \/apache2-default) when you go to \/\r\n                #RedirectMatch ^\/$ \/apache2-default\/\r\n        &lt;\/Directory&gt;<\/pre>\n<p>You need to modify the line containing\u00a0<tt>AllowOverride\u00a0None<\/tt>\u00a0to read\u00a0<tt>AllowOverride\u00a0All<\/tt>. This tells Apache that it&#8217;s okay to allow<tt>.htaccess<\/tt>\u00a0files to over-ride previous directives. You must\u00a0<em>reload<\/em>\u00a0Apache before this change will have an effect:<\/p>\n<pre>sudo \/etc\/init.d\/apache2 reload<\/pre>\n<p>2009.12.08 note: in the LAMP download about a week ago with Ubuntu 9.10 (Karmic) the default configuration file was\u00a0<tt>\/etc\/apache2\/sites-available\/000-default<\/tt>\u00a0and it included\u00a0<a href=\"https:\/\/help.ubuntu.com\/community\/AllowOverride\">AllowOverride<\/a>\u00a0None under\u00a0<tt>&lt;Directory\u00a0\/&gt;<\/tt>\u00a0in addition to\u00a0<tt>&lt;Directory\u00a0\/var\/www\/&gt;<\/tt>. Also, directories in\u00a0<tt>\/www\/var\/<\/tt>\u00a0containing\u00a0<tt>.htaccess<\/tt>\u00a0files defaulted to not giving the Apache server read access, resulting in the Apache error<\/p>\n<ul>\n<li><em>(13)Permission denied: \/var\/www\/webapp\/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable<\/em>.<\/li>\n<\/ul>\n<p>To fix,\u00a0<tt>$\u00a0sudo\u00a0nautilus<\/tt>\u00a0then right click on the directory with the .htaccess file, select Properties, then select Permissions, and give the user group you log in as at least read permission.<\/p>\n<p>See\u00a0<a href=\"http:\/\/httpd.apache.org\/docs\/2.0\/mod\/core.html#allowoverride\">http:\/\/httpd.apache.org\/docs\/2.0\/mod\/core.html#allowoverride<\/a>\u00a0for more info on\u00a0<tt>AllowOverride<\/tt>.<\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"Password-Protect_a_Directory_With_.htaccess\">Password-Protect a Directory With .htaccess<\/h2>\n<p><strong>Warning: On at least some versions of Ubuntu,\u00a0<tt>.htaccess<\/tt>\u00a0files will not work by default. See EnablingUseOfApacheHtaccessFiles for help on enabling them.<\/strong><\/p>\n<p>Create a file called\u00a0<tt>.htaccess<\/tt>\u00a0in the directory you want to password-protect with the follwing content:<\/p>\n<pre>AuthUserFile \/your\/path\/.htpasswd\r\nAuthName \"Authorization Required\"\r\nAuthType Basic\r\nrequire valid-user<\/pre>\n<p>instead of\u00a0<tt>valid-user<\/tt>, you can also add the users you want directly<\/p>\n<p>If you want to password protect just a single file in a folder add the following lines to the\u00a0<tt>.htaccess<\/tt>\u00a0file:<\/p>\n<pre>&lt;Files \"mypage.html\"&gt;\r\n  Require valid-user\r\n&lt;\/Files&gt;<\/pre>\n<p>Then create the file\u00a0<tt>\/your\/path\/.htpasswd<\/tt>\u00a0which contains the users that are allowed to login and their passwords. We do that with the<tt>htpasswd<\/tt>\u00a0command:<\/p>\n<pre>htpasswd -c \/path\/to\/your\/.htpasswd user1<\/pre>\n<p>The\u00a0<tt>-c<\/tt>\u00a0flag is used only when you are creating a new file. After the first time, you will omit the\u00a0<tt>-c<\/tt>\u00a0flag, when you are adding new users to an already-existing password file. Otherwise you will overwrite the file!!<\/p>\n<p>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.<\/p>\n<p>Finally we need to add the following lines to\u00a0<tt>\/etc\/apache2\/apache2.conf<\/tt>:<\/p>\n<pre>&lt;Directory \/your\/path&gt;\r\nAllowOverride All\r\n&lt;\/Directory&gt;<\/pre>\n<p>You have to adjust\u00a0<tt>\/your\/path\/.htpasswd<\/tt><\/p>\n<p>Restart your webserver:<\/p>\n<pre>sudo \/etc\/init.d\/apache2 restart<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"Troubleshooting\">Troubleshooting<\/h3>\n<p>If you can&#8217;t access your stuff and the dialog keeps popping up, check that you entered the username and password correctly. If it still doesn&#8217;t work, check the path to your\u00a0<tt>.htpasswd<\/tt>\u00a0and make sure the path specified in the\u00a0<tt>AuthUserFile\u00a0directive<\/tt>\u00a0is correct. Also make sure that both the<tt>.htpasswd<\/tt>\u00a0and\u00a0<tt>.htaccess<\/tt>\u00a0files are readable by the web server user\u00a0<tt>chmod\u00a0644<\/tt>\u00a0should do the trick!<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"Example\">Example<\/h3>\n<p>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:<\/p>\n<pre>AuthUserFile \/your\/path\/.htpasswd\r\nAuthName \"Authorization Required\"\r\nAuthType Basic\r\nOrder Allow,Deny\r\n&lt;Files myfile1.html&gt;\r\n Order Allow,Deny\r\n require valid-user\r\n&lt;\/Files&gt;\r\n\r\n&lt;Files myfile2.html&gt;\r\n Order Deny,Allow\r\n&lt;\/Files&gt;<\/pre>\n<p>&nbsp;<\/p>\n<h2 id=\"Redirect_requests_using_.htaccess_and_mod_rewrite\">Redirect requests using .htaccess and mod_rewrite<\/h2>\n<ol type=\"1\">\n<li>Make sure Apache .htaccess is enabled (by default it is enabled in Ubuntu)<\/li>\n<li>Make sure the Apache module\u00a0<tt>mod_rewrite<\/tt>\u00a0is enabled. Execute:<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<pre>sudo a2enmod rewrite<\/pre>\n<p>..and see if rewrite is listed here:<\/p>\n<pre>sudo apache2ctl -M<\/pre>\n<p>and then you can redirect requests using\u00a0<a href=\"https:\/\/help.ubuntu.com\/community\/RewriteRules\">RewriteRules<\/a>. Example:<\/p>\n<pre>RewriteEngine On\r\n\r\nRewriteCond %{REQUEST_FILENAME} !-f\r\nRewriteCond %{REQUEST_FILENAME} !-d\r\nRewriteRule ^(.*)$ index.php?_REQUEST=$1 [L]<\/pre>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>EnablingUseOfApacheHtaccessFiles WARNING: This page was written based on Ubuntu 10.04 (Lucid), although the same may apply to other releases. You are encouraged [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1587","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1587"}],"version-history":[{"count":0,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1587\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}