homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

rewrite rule if some folder has no index file
I want to show my own php page if a folder has no index file

 11:54 am on Jan 3, 2010 (gmt 0)

I am using these rewrites for a domain parking site.
Actual structure on disk is:


The domains on disk can contain images or other files but if domain folder on disk has no index file I would like to show a php page /domains/index.php
What I tried is this but it is wrong:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^domains/([^/]*)/$ /domains/index.php?domain=$1

What is the right approach? Can I test something like this?
if !file_exists index.php [AND]
if !file_exists index.html
then go to /domains/index.php?domain=$1
If that would be correct, how to I write this in .htaccess?

Thank you.



 8:40 pm on Jan 3, 2010 (gmt 0)

You shouldn't even have to use mod_rewrite, as mod_dir can take care of this for you:

Options -Indexes
DirectoryIndex /domains/index.php



 9:41 pm on Jan 3, 2010 (gmt 0)

Hello jdMorgan, I don't know where (in what order) to put that and I am unsure if it would work, I have some more code that needs to integrate. I made some tests and I was not able to use that line with my code.
Is it still possible to do it by a RewriteCond ?

RewriteCond if ! file_exists /domains/(the_domain)/index.php
RewriteRule ^domains/([^/]*)/$ /domains/index.php?domain=$1

What is correct way to write that cond on first line?


 10:03 pm on Jan 3, 2010 (gmt 0)

I found a solution that seems to do the job, can someone tell me if anything would be wrong with this?

Considering above in my code I have this, redirecting all to local folder (without "www"):

RewriteCond %{HTTP_HOST} ^(www.)?([^.]*)\.(.*)$ [NC]
RewriteRule ^(.*)$ /domains/%2.%3/$1 [L,QSA]

Then would this test be ok to test for index file exists?

RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^domains/([^/]*)/$ /domains/?domain=$1

It appears to work ok.


 10:10 pm on Jan 3, 2010 (gmt 0)

None of those are 'redirects'. They are all rewrites.

Every rule needs [L] after, but really, the code that jd supplied should work.


 11:00 pm on Jan 3, 2010 (gmt 0)

I think you mentioned that you also wanted to check for .html as well...

RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^domains/([^/]*)/$ /domains/?domain=$1

A lot of the 'design' decisions here are dictated by your URL-space. So if you have other 'index page' URLs that resolve to the /domains subdirectory that you *do not* want to 'rewrite' to /domains/index.php, then you can't use the mod_dir code in /domains/.htaccess, since it will be applied before any mod_rewrite code in that .htaccess file...

But if there are no other 'index' page-paths under /domains, then it should work just fine in /domains/.htacess



 11:42 pm on Jan 3, 2010 (gmt 0)

Yes, that is the code I use now and it appears to work in all tests and situations, url formats I tested.

About the "mod_dir" that you mentioned in this last post, I didn't get it; from what I read from you, maybe you thought my .htaccess is under /domains/.htaccess ?! But the htaccess is in root. Is that irrelevant if I use the last 3 htaccess lines above?


 1:17 am on Jan 4, 2010 (gmt 0)

You can only use the mod_dir solution in /domains/.htaccess... unless you want *all* index requests site-wide to go to that same specified index.php file.

In case it's not clear, every directory can have its own .htaccess file both for efficiency (however you define it on the scale between execution efficiency and administration efficiency) and to control the 'scope' of the directives in that .htaccess file.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved