1

可以通过 .htaccess 将查询发送到另一个文件吗?稍微解释一下:

index.php像这样处理所有页面:

switch($page) {
    case 'index':
        require_once START . '/includes/pages/index.php';
    break;

    case 'another':
        require_once START . '/includes/pages/another.php';
    break;
       and so on...

在 .htaccess 中:

RewriteRule ^index$ index.php?page=index [L]
RewriteRule ^another$ index.php?page=another [L]

链接(例如localhost/84ss7d41):

RewriteRule ^(.*)$ index.php?QUERY_STRING=$1 [L]

所以在/includes/pages/index.php

if ($_GET['QUERY_STRING']) {
$_SERVER['QUERY_STRING'] = $_GET['QUERY_STRING'];
$query_string = check_input($_SERVER['QUERY_STRING']);
    //checking/validating processing further...

如何使像这样的链接localhost/84ss7d41以与现在相同的方式工作,但没有index.php?如果发生某些事情(文件被意外删除),我正在尝试分离我的“项目”,检查/验证数据的工作方式相同,没有主脚本,就像:

if (main project file index.php/ || /includes/pages/index.php doesn't exist) {
  check/validate data with reserve.php
}
4

1 回答 1

1
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*) /index.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) /reserve.php [L]

这会将所有与现有文件(例如图像)不匹配的请求重定向到index.php,然后您可以使用该位置$_SERVER["REQUEST_URI"]来决定如何操作。这是前端控制器模式http://en.wikipedia.org/wiki/Front_Controller_pattern的实现

此外,如果index.phpapache 找不到,则请求将被转发到reserve.php

虽然,建立这种冗余不一定是“正确”的事情(如果reserve.php 也丢失了怎么办?)。实施适当的访问控制index.php可能是一种更好的方法。

于 2012-03-15T23:14:51.873 回答