Virtual Host Configuration and mod_rewrite – Removing PHP Extension and Adding Forward Slash

.htaccessmod-rewritevirtualhost

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    

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

    </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?

NOTE

See my answer below

50 points to anyone who can shed additional light on this answer. I started a bounty and then found my answer but am still learning so any good tips can be worth +50.

Best Answer

Maybe someone will get a more detailed answer for this particular case, but as a more general advice, you can use the RewriteLog and RewriteLogLevel directives to get detailled information about which rule is processed and what is the result of the rewriting.

With that information, you'll be able to find the missing rule, or at least, send us the log output to have more details.

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog

Related Topic