Error 404 on virtual hosts using custom .htaccess files
P粉729198207
P粉729198207 2023-09-05 10:28:49
0
2
728
<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>
P粉729198207
P粉729198207

reply all(2)
P粉151720173

In your code, REQUEST_FILENAME requires a file with a php extension to perform the rewrite.

Try this:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC,L]
P粉009828788

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:

<Directory /var/www/repos/pcts>
    # Enable .htaccess overrides
    AllowOverride All

    # Allow user access to this directory
    Require all granted
</Directory>

If desired, you can further restrict the .htaccess overrides (see reference link below)

refer to:

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template