Getting .htaccess to work on localhost

A

Anonymous

Guest
I am attempting to get .htaccess to work for localhost on Linux. I am using ZorinOS 15.3 (which is based on Ubuntu). I am trying to get .htaccess to work for the purpose of testing/debugging PHP code that I intend to upload to my web host when I'm finished. The following is my .htaccess file in /var/web/html which I validated with https://htaccess.madewithlove.com/

Code:
RewriteEngine On
RewriteRule ^blog/(!(\.))* blog/blog.php?path=${1}

I tried putting the following text in /etc/apahce2/sites-available/000-default.conf per https://linuxcommando.blogspot.com/2014/03/how-to-enable-modrewrite-for-apache-web.html

Code:
 <?Directory /var/www/>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  Order allow,deny
  allow from all
 <?/Directory>

I included this within the <VirtualHost> tags. When I visit any page on localhost (using Chromium Version 96.0.4664.110) I get an error saying:
Code:
This site can’t be reached
127.0.0.1 refused to connect.
Try:

Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED

I removed the code I added to /etc/apahce2/sites-available/000-default.conf and restarted Apache. I wrote a PHP file with
Code:
print_r(apache_get_modules());
and visited it in Chromium. Index 26 was mod_rewrite. The full output is:

Code:
Array ( [0] => core [1] => mod_so [2] => mod_watchdog [3] => http_core [4] => mod_log_config [5] => mod_logio [6] => mod_version [7] => mod_unixd [8] => mod_access_compat [9] => mod_alias [10] => mod_auth_basic [11] => mod_authn_core [12] => mod_authn_file [13] => mod_authz_core [14] => mod_authz_host [15] => mod_authz_user [16] => mod_autoindex [17] => mod_deflate [18] => mod_dir [19] => mod_env [20] => mod_filter [21] => mod_mime [22] => prefork [23] => mod_negotiation [24] => mod_php7 [25] => mod_reqtimeout [26] => mod_rewrite [27] => mod_setenvif [28] => mod_status )

Next I added
Code:
AccessFileName .htaccess
to /etc/apahce2/sites-available/000-default.conf per https://unix.stackexchange.com/questions/18824/localhost-htaccess-not-working-on-ubuntu. I still got the error. When I try the url http://127.0.0.1/blog/page/1 I get the following in /var/log/apache2/access.log:

127.0.0.1 - - [20/Dec/2021:07:33:02 -0800] "GET /blog/page/1 HTTP/1.1" 404 488 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"

How can I get .htaccess to work on localhost?
 
I got it working. I think what made it work is adding the following to /etc/apache2/sites-available/000-default.conf between the <VirtualHost> tags:
Code:
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
AccessFileName .htaccess
 
Back
Top