search

Home  >  Q&A  >  body text

Error 404 on virtual hosts using custom .htaccess files

<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粉729198207P粉729198207448 days ago697

reply all(2)I'll reply

  • P粉151720173

    P粉1517201732023-09-06 08:47:09

    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]

    reply
    0
  • P粉009828788

    P粉0098287882023-09-06 00:02:49

    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:

    reply
    0
  • Cancelreply