English 中文(简体)
从 PHP 内部读取 mod_rewriter 配置
原标题:read mod_rewrite config from within PHP

我有一套 PHP 软件, 用 mod_ rewrite 做漂亮的事情。 但同样的软件应该运行在服务器上, 服务器上没有安装 mod_ rewrite 。 如果安装了 mod_ rewrite 并且应用了某种规则, 我可以检查我的php 代码吗?

例如,像这样的事情:

    if ((mod_rewrite is enabled) and (mod_rewrite_rule is OK)){
        return  createBeautifullLink();
    }else{
        return createUglyLink();
    }

提前感谢

最佳回答

使用此选项 :

以 < 坚固>.ht access

<IfModule mod_rewrite.c>
   # inform php that mod_rewrite is enabled
   SetEnv HTTP_MOD_REWRITE on
   ...

以 < 强度 > PHP :

$mod_rewrite = FALSE;
if (function_exists("apache_get_modules")) {
   $modules = apache_get_modules();
   $mod_rewrite = in_array("mod_rewrite",$modules);
}
if (!isset($mod_rewrite) && isset($_SERVER["HTTP_MOD_REWRITE"])) {
   $mod_rewrite = ($_SERVER["HTTP_MOD_REWRITE"]=="on" ? TRUE : FALSE); 
}
if (!isset($mod_rewrite)) {
   // last solution; call a specific page as "mod-rewrite" have been enabled; based on result, we decide.
   $result = file_get_contents("http://somepage.com/test_mod_rewrite");
   $mod_rewrite  = ($result=="ok" ? TRUE : FALSE);
}

The first (apache) can be disabled by server, the second custom one will exist in $_SERVER only if mod_env is installed. So what i think as best solution is to create a fake url redirection in your .htaccess that points to some file of yours (that return simply an "ok") and call that with the redirection from a .php; if returns "ok", you can use clean urls... The redirection code in .htaccess might looks like:

<IfModule mod_rewrite.c>
   ...
   RewriteEngine on
   # fake rule to verify if mod rewriting works (if there are unbearable restrictions..)
   RewriteRule ^test_mod_rewrite/?$    index.php?type=test_mod_rewrite [NC,L]
问题回答

(如果PHP在工业和工业部中是<强 > 不 < /强>,则以下作品)

尝试一下这个 :

if (function_exists( apache_get_modules )) {
   $modules = apache_get_modules();
   $mod_rewrite = in_array( mod_rewrite , $modules);
} else {
   $mod_rewrite =  getenv( HTTP_MOD_REWRITE )== On  ? true : false ;
}

: < a href="" https://stackoverflow.com/ questions/1301118/ how-to- detect-mod-rewrite- without- apache-get-modules" > 如何检测没有 apache_get_modules () 的mod_restrite?

信用单位为""http://christian.roy.name/blog/detecting-modrewrite-using-php" rel="没有跟随 Noreferrer" > Christian Roy





相关问题
Using SimplePie with CodeIgniter and XAMPP

I am using CodeIgniter 1.7.2 with XAMPP 1.7.2 on a Windows computer. I am trying to make use of SimplePie. I followed all the instructions I could find: a copy of simplepie.inc is in my applications/...

Multiple Sites with common files

I have developed over 50 sites that all use the exact same files other than CSS and IMAGES, I currently duplicate the files each time I create a new site and upload different css and images. What ...

http server validation

I finish a litle http server, writing from scratch. I would like to be sure that my imlementation is conforme to the HTTP specifications. W3C give us tools for HTML/XML conformance, but i see nothing ...

热门标签