【问题标题】:.htaccess Multiple redirects.htaccess 多个重定向
【发布时间】:2009-12-08 14:33:38
【问题描述】:

这是我的目录结构

/home
    /template
    /classifieds
    /listings

我想隐藏 /template 并想在 home 的 template 下显示所有文件。比如 /home/template/home.php 应该是 www.example.com/home.php 或者 /home/template/style.css 应该是 www.example.com/style.css

如果有人试图访问 example.com/template/.php,则应将其返回到 example.com/.php,如果不存在,则应返回 404.php。

这是我的 htaccess 来处理。

<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

还有我的 php 代码

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

然而 我添加了更多内容,例如 /classified 和 /listings 应该按原样列出,即 example.com/classified/*.php

现在当我尝试访问 /answers/home.php 时,它会直接打开 home.php 而不是打开 index.php

这是我的新代码

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

那么 htaccess 应该是什么样的?

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    我知道这不是对您问题的直接回答,但似乎更合理地组织您的文件结构以匹配您的网络结构。我建议:

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

    从我在您的帖子中可以看出,完全没有理由使用 mod_rewrite。

    但如果你真的想知道:

    RewriteCond %{REQUEST_URI} !^/classifieds/
    RewriteCond %{REQUEST_URI} !^/listings/
    

    【讨论】:

    • 我支持“重新组织您的目录”部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多