Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule matches for unknown reason

Matches root directory but don't know why

         

Merganser

4:15 am on Feb 3, 2012 (gmt 0)

10+ Year Member



I have a RewriteRule of the following form (there are some RewriteConds above it as well):

RewriteRule ^$|\.php - [F]

My intent is for it to match my root directory (^$) and any .php pages (\.php). My root directory (i.e., www.MyWebsite.com) has a hidden index.php in it. The RewriteRule works so I got the effect I wanted but, I later noticed that it also works the same if I just use the \.php. In other words, the \.php seems to be getting a match for www.MyWebsite.com (even when index.php is not explicitly listed in the URL).

I was wondering if someone could help me understand why. Not a critical issue but just wanting to understand.

lucy24

8:16 am on Feb 3, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are you on shared hosting? I used to have exactly the same thing happen-- with me it was html. In fact it went one step further. If I wanted something to apply to a directory, even to the top-level www.example.com, I had to include \.html$ in the Rule. I think this means that mod_dir was running before mod_rewrite. I say "was" because in my case it seems to have changed.

If you don't have direct access to the information, you can always make up some kind of rewrite test to see which module runs first.

Merganser

5:23 am on Feb 4, 2012 (gmt 0)

10+ Year Member



Lucy,

I am on shared hosting.

I am not familiar with mod_dir. Can you give me some starter info or point me to where I could read up on it.

g1smd

7:56 am on Feb 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The Apache manual lists all of the Apache modules and details their functionality.

It's not an easy read though.

lucy24

9:34 am on Feb 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



[httpd.apache.org...]

Short version: mod_dir (for "directory") handles directory-slash redirects and named index files. Not auto-indexing; that's mod_autoindex.

So it does two things. If you ask for

www.example.com/foobar

it checks whether "foobar" is the name of a directory, and if yes, redirects to

www.example.com/foobar/

Then its second function kicks in and it checks whether /foobar/ contains a file with any of a short list of names. Your host's config file contains this list. At a minimum: index.html index.htm index.php

So you see that if mod_dir does its stuff before mod_rewrite, then mod_rewrite will never see a request ending in / It will only see index.html. Or .php or whatever you use.

There are further complications if a particular directory is auto-indexed, since that's a different module running at a different time. And if you need your index files to have a name other that what's listed in the config file, you have to put something in your own htaccess.

Apache has some sinister warnings about what can happen if you turn off the directory-slash redirect while keeping auto-indexing active. But read the paragraph very slowly or it will give you a headache.

g1smd

7:09 pm on Feb 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's not all that many places where the Apache manual issues "sinister warnings" but ignore them at your peril.