尽管我对干净 URL 工作方式的有根据的猜测是正确的,但我还是编码干净 URL 的新手。
我的第一步是创建一个兼容性工具来测试服务器上是否启用了 mod_rewrite。我成功地创建了我需要的三个文件:PHP 工具文件、.htaccess 文件和要重写的文件 URL。
基本的 RewriteRule 已就位,当我转到重写的 URL 时,它会成功重定向。但是,在尝试使用 PHP INCLUDE/REQUIRE 甚至 FILE_EXISTS 函数引用重写的 URL 时,它会失败返回。这是不可能的还是我的处理方式错误?
如果有必要,我很乐意发布代码(我是通过智能手机编写的),但现在我仍在尝试确定它是否可能,以及如果可以的话,实现它的最佳方法是什么。
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .*rewrite_test\.php rewrite.php [nc]
</IfModule>
重写.php:
<?php
$mod_rewrite = TRUE;
echo "test";
?>
测试器.php:
<?php
// Check if APACHE MOD_REWRITE is enabled for clean URLs
$mod_rewrite = file_exists('rewrite_test.php');
echo ($mod_rewrite ? "TRUE" : "FALSE");
?>
或 tester.php 代码替代:
<?php
// Check if APACHE MOD_REWRITE is enabled for clean URLs
$mod_rewrite = FALSE;
include_once('rewrite_test.php');
echo $mod_rewrite;
?>
最佳答案
Apache 重写规则对 include
、require
或 file_exists
没有任何影响,因为它们使用的是服务器上的路径,而不是 URL除非您启用 allow_url_fopen
和 specify a full URL .
编辑:
如果您只是想检查 mod_rewrite
在您的服务器上是否可用,那更容易:使用 apache-get-modules()
。这很简单:
$mod_rewrite = (
function_exists('apache_get_modules') &&
in_array('mod_rewrite', apache_get_modules())
);
关于php - .htaccess RewriteRule 与 PHP INCLUDE 或 REQUIRE 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11119491/