开发者

.htaccess Multiple redirects

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 ?


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/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜