Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I changed this:

Options Indexes FollowSymLinks Includes

to this:

Options Indexes FollowSymLinks MultiViews

and now it works as expected... Here is the rule I went with to remove PHP extension:

<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
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule (.*)/$ /$1.php

    # add trailing slash to all URLs
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.example.com/$1/ [R=302,L]

</IfModule>
share|improve this 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

share|improve this answer
    
Thank you. Yes, I do have rewrite logs and I'm looking at them right now. –  nicorellius Nov 7 '13 at 22:57
    
I discovered some interesting things when peaking into these logs. Basically, I reverted back to no rules, confirmed everything was working as expected, eg, going example.com/dir/ actually gave my the index and now I'm building up, one rule at a time. I'm still stuck, but your advice was definitely worth +1. Thanks. –  nicorellius Nov 8 '13 at 20:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.