重写 htacces 找不到文件


Rewrite htacces can't find files

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

这是我的访问:

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,因此函数找不到['p']但我不确定,我不知道是不是这样我怎么能只重写我的/pages文件夹中的文件

index.php

第二次传递时被重写拾取并发送到您的页面目录中没有相应页面index.php?p=index,因此获取404.php

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

您只需将 .htaccess 更改为:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9'-]+)'.php$ index.php?p=$1 [L]

参见 Apache 的 RewriteCond 手册。