Directories Throwing 404 Errors - Virtual Host Configuration and mod_rewrite

Posted by nicorellius on Server Fault See other posts from Server Fault or by nicorellius
Published on 2013-11-07T20:12:51Z Indexed on 2013/11/07 21:58 UTC
Read the original article Hit count: 160

On my production server, things are fine: PHP extension removal and trailing slash rules are in place in my .htaccess file.

But locally, this isn't working (well, partially, anyway). I'm running Apache2 with a virtual host for the site in question. I decided to not use the .htaccess file in this case and just add the rules to the httpd-vhosts.conf file instead, which, I've heard, if possible on your server, is a better way to go.

The virtual host is working and the URL I use for my site is like this:

devserver:9090

Here is my httpd-vhosts.conf file:

NameVirtualHost *:9090

# for stuff other than this site
<VirtualHost *:9090>
    ServerAdmin admin@localhost
    DocumentRoot "/opt/lampstack/apache2/htdocs"
    ServerName localhost
</VirtualHost>

# for site in question
<VirtualHost *:9090>
    ServerAdmin admin@localhost
    DocumentRoot "/opt/lampstack/apache2/htdocs/devserver"
    ServerName devserver

    <Directory "/opt/lampstack/apache2/htdocs/devserver">
        Options Indexes FollowSymLinks Includes
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>

    <IfModule rewrite_module>
        RewriteEngine ON    
        # remove PHP extension and add trailing slash
        # note - this doesn't work for directories, and throws 404
        # TODO - fix so directories use index.php
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
        RewriteRule (.*)\.php$ /$1/ [R=302,L]

        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule (.*)/$ /$1.php [L]

        RewriteCond %{REQUEST_FILENAME}.php -f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule .*[^/]$ /$0/ [R=302,L]
    </IfModule>

    # error docs
    ErrorDocument 404 /errors/404.php

</VirtualHost>

The problem I'm facing is that when I go to directories on the site, I get a 404 error. So for example, this:

devserver:9090/page.php

goes to

devserver:9090/page/

but going to a directory (that has an index.php):

devserver:9090/dir/

throws 404 error page.

If I type in devserver:9090/dir/index.php I get devserver:9090/dir/index/ and the contents I want appear...

Can anyone help me with my rewrite rules?

© Server Fault or respective owner

Related posts about .htaccess

Related posts about mod-rewrite