views:

119

answers:

1

Hello, this is my directory structure

/home
    /template
    /classifieds
    /listings

I want to hide /template and want to show all files under template on home. like /home/template/home.php should be www.example.com/home.php or /home/template/style.css should be www.example.com/style.css

And Someone trying to access example.com/template/.php should be thrown back to example.com/.php if it exists or 404.php if it doesnt.

This was my htaccess to handle that.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/template/.*php
RewriteRule . /$1 [NC,R] 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Options -Indexes

And my php code

if (!file_exists("template/" . $page))
    include_once "template/404.php";
else
    include_once "template/" . $page;

However I have added more things like /classified and /listings should be listed as it is, i.e example.com/classified/*.php

Now when I try to access /answers/home.php it directly opens the home.php rather than opening index.php

This is my new code

if (!file_exists($segments[1] . '/' . $page))
     include_once "template/404.php";
else
    include_once $segments[1] . '/' . $page;

SO what should be the htaccess like ?

+1  A: 

I know this is not a direct answer to your question, but it seems to make more sense to logically organize your file structure to match your web structure. I would suggest:

/home
  /public_html
     /classifieds
     /listings
     404.php
     index.php

From what I can tell in your post, there is absolutely no reason to be using mod_rewrite at all.

But if you really want to know:

RewriteCond %{REQUEST_URI} !^/classifieds/
RewriteCond %{REQUEST_URI} !^/listings/
Kenaniah
I second the 're-org your directories' part.
Don