4

My hosting company recently upgraded me from Apache 1 to Apache 2, and I've started seeing some quite different behaviour with my mod_rewrite stuff.

Here's my .htaccess file:

DirectoryIndex blog.html

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # Rewrite current-style URLs of the form 'showpage.php?url=x'.
  RewriteRule ^(.*\.html)$ showpage.php?url=$1 [L,QSA]

</IfModule>

Now, previously with Apache 1, if you went to http://mysite.com/ then the DirectoryIndex would first take effect (http://mysite.com/blog.html) and then the RewriteRule would turn that into http://mysite.com/showpage.php?url=/blog.html

Now with Apache 2, if you go to http://mysite.com/blog.html it gets rewritten as expected, but if you go to http://mysite.com/ it serves you the vanilla blog.html file, without rewriting it to showpage.php. So the RewriteRule is being applied before the DirectoryIndex kicks in.

Besides adding an extra rule explicitly to catch the root page (which will be tedious since I'd have to take account of all subdirectories which also have a DirectoryIndex) does anybody know a way to make Apache 2 apply the RewriteRule after applying the DirectoryIndex?

andygeers
  • 6,909
  • 9
  • 49
  • 63
  • this works for me with apache 2.2.13. which apache version are you using? – ax. Sep 02 '09 at 16:49
  • I'd love to hear if you ever got a good answer to this. I'm stuck reimplementing server core functionality (test for directory existence, append trailing slash) in a `RewriteCond`, and worse, I have multiple `index.*` things in `DirectoryIndex`, and it looks like I have to add `-f` tests for them too. Kludges are piling up around the problem that rewrite is being applied at the wrong stage. –  Oct 01 '13 at 18:18

1 Answers1

1

Try this rule instead of the DirectoryIndex directive:

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/?$ $1/blog.html
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • i think he doesn't want to remove/replace the DirectoryIndex. – ax. Sep 03 '09 at 06:23
  • @ax: Then he should first try it additionally to the `DirectoryIndex` directive. – Gumbo Sep 03 '09 at 07:53
  • As a note, remember that `%{REQUEST_FILENAME}` returns the path situated at docroot. Therefore, you *may* have to prepend the docroot to it like so `RewriteCond /var/www%{REQUEST_FILENAME} -d` – puk Apr 10 '12 at 18:11
  • It's been so long since I did this that I can't really remember how I solved this - but it's quite possible that I did indeed end up using this approach of using a RewriteRule in place of a DirectoryIndex, so I'll go ahead and accept this answer. – andygeers Oct 02 '13 at 08:36