apache重写规则将所有.php文件的请求传递到特定文件

时间:2010-04-17 15:46:53

标签: apache mod-rewrite

我正在开发一个简单的网站。我正在使用模板文件(template.php),它具有所有共享的HTML,并接受一个参数(比如'page')。然后打开页面文件并包含页面本身。这样的事情:(代码只是一个原型。不需要验证/过滤。)

<?php
//template.php
?>
<html>
    <body>
      <div>my web site</div> <!-- shared by all pages -->
      <div> <!-- specific page contents -->
         <?php include($_REQUEST['page']); ?>
      </div>
    </body>
</html>

.htaccess文件应该是什么样的,所以对PHP文件的请求通过template.php文件,并带有'page'参数作为所请求文件名的名称?

1 个答案:

答案 0 :(得分:0)

尝试此规则:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ template.php?page=$0 [L]

其他RewriteCond条件是排除可以映射到现有文件(-f)或目录(-d)的请求。