重写htacces无法找到文件

时间:2016-08-03 08:49:59

标签: javascript php jquery ajax .htaccess

我创建了一个ajax网站,从index.php内的/ pages文件夹中调用php页面,所以我在htaccess中重写了我的索引中的所有页面返回所以ajax运行良好但是当点击刷新页面按钮或首次加载时,该功能无法找到文件,一直返回404.php页面:

这是我的htaccess:

Options +FollowSymLinks

RewriteEngine On

RewriteBase /
RewriteRule ^([a-zA-Z0-9\-]*).php$ index.php?p=$1 [L]

这是我的php函数调用我的页面:

<div id="ajax-container">
<?php
 $d = "pages/";
 if (isset($_GET['p'])) {
    $p = strtolower($_GET['p']);
    if (preg_match("/^[a-z0-9\-]+$/", $p) && file_exists($d . $p . ".php")) {
        include $d . $p . ".php";
    } else {
        include $d . "404.php";
    }
 } else {
    include $d . "home.php";
 }
?>
</div>

我认为问题来自于我的重写,因为它重写了所有页面的php,所以我认为它还会重写我的index.php,因此该函数无法找到{{} 1}}但我不确定,我不知道这是不是我怎么能只重写['p']文件夹中的文件

1 个答案:

答案 0 :(得分:2)

index.php在第二次传递时被您的重写攻击并被发送到<{1}},而<{1}}在 pages 目录中没有相应的页面,所以提取index.php?p=index

正常避免这种情况的方法是在.htaccess中添加 not file not directory 的条件(从而防止URL到文件或者被重写实际存在的目录。)

您只需将.htaccess更改为:

即可
404.php


请参阅Apache的RewriteCond手册。

相关问题