Forum Moderators: phranque

Message Too Old, No Replies

htaccess - disallow access by filename, only directory

Throw 404 error if file accessed by its name

         

silkfire

11:46 pm on Apr 25, 2010 (gmt 0)

10+ Year Member



Is this possible? I've been struggling the whole weekend this htaccess nightmare...

Rules:

1. www.example.com/index.php -> throw 404 error (the file does exist)

2. www.example.com -> load the index.php (even if its blocked by direct access)

3. www.example.com/index -> throw 404 error


Currently, all these addresses give me the same site; I have no idea why "www.exaple.com/index" works as a valid address by the way.

These rules must work on the whole domain (my htaccess is in the root directory).

So what I want to basically do is make all URL with something after the slash (/) return 404 (not "Forbidden") but still allow access without anything after the slash. Must also work for all folders and any file (my server automatically knows that "index.php" is the default file to load).

Huge thanks in advance.

[edited by: jdMorgan at 12:58 am (utc) on Apr 26, 2010]
[edit reason] Please use eaxmple.com only. [/edit]

jdMorgan

1:08 am on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This set of requirements is impractical and violates the intent of the HTTP response codes. Therefore it will likely lead to big trouble with search engines and other clients. Not to mention that as described, your server will be incapable of serving any images or CSS, JS, robots.txt, or sitemap.xml files...

I suggest:

www.example.com/ : Use DirectoryIndex (Apache mod_dir) to load index.php
www.example.com/index.php : 301 redirect the client to www.example.com/ (Apache mod_rewrite)
www.example.com/index : 301 redirect to www.example.com/ (mod_rewrite) or return default 404-Not Found

If you disable MultiViews and AcceptPathInfo, then your current "mysterious" index-page loading will stop.

Redirecting example.com/index.php back to "/" requires that you use a RewriteCond to check that this request is coming direct from a client, rather than being the result of the internal rewrite performed by mod_dir to implement your DirectoryIndex function. Search this forum for "redirect index to slash" and similar phrases to see the code -- posted dozens of times here.

Jim

silkfire

7:21 am on Apr 26, 2010 (gmt 0)

10+ Year Member



I'm running Apache 1.3 unfortunately which doesn't support AcceptPathInfo, could you help me make so that:

/folder/index => 404

and

/folder/index => redirect to /folder/

?

thanks again, jd!

g1smd

10:50 am on Apr 26, 2010 (gmt 0)

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



The redirect
index.<anything>
code has been shown hundreds of times before.

Post your intended code here for discussion.

jdMorgan

1:44 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Example code: [webmasterworld.com...]

Jim