1

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.

2 Answers 2

2
+50

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

2
  • Thank you. Yes, I do have rewrite logs and I'm looking at them right now. Commented Nov 7, 2013 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. Commented Nov 8, 2013 at 20:43
0

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> 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.