Error 404 on virtual hosts using custom .htaccess files
P粉729198207
2023-09-05 10:28:49
<p>I installed apache2 on my local Linux server. It has a virtual host named <code>pcts.local</code> and its root directory is <code>/var/www/repos/pcts/</code>. Within the root of pcts.local is a .htaccess file which attempts to rewrite the url to include .php if not given as follows: </p>
<pre class="brush:php;toolbar:false;">http://pcts.local/ -> http://pcts.local/index.php
http://pcts.local/contact -> http://pcts.local/contact.php</pre>
<p>The problem is, <code>http://pcts.local/contact</code> gives error 404, but <code>http://pcts.local/contact.php</code> Out of 200. </p>
<h3>Virtual host configuration: </h3>
<pre class="brush:php;toolbar:false;"><VirtualHost *:80>
ServerName pcts.local
ServerAdmin webmaster@localhost
DocumentRoot /var/www/repos/pcts
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost></pre>
The <h3>.htaccess file is in <code>/var/www/repos/pcts/</code></h3>
<pre class="brush:php;toolbar:false;">RewriteEngine On
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(. )$ $1.php [NC,L]</pre>
<p>Thanks in advance for your help! </p>
In your code, REQUEST_FILENAME requires a file with a php extension to perform the rewrite.
Try this:
If this is your complete configuration, your
.htaccess
file will not be processed.You have not enabled
.htaccess
overrides for a specific directory. (That is, you have not enabled parsing of.htaccess
files.) By default,.htaccess
overrides are disabled.But you didn't enable access to this area of the file system either? Have you done this elsewhere in the server configuration? !
You should have a related
section inside the
container like this:If desired, you can further restrict the
.htaccess
overrides (see reference link below)refer to: