如何检查 php 中是否启用了 mod_rewrite?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9021425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:00:22  来源:igfitidea点击:

How to check if mod_rewrite is enabled in php?

phpapachemod-rewriteiis

提问by Ties

I was wondering if it is possible to check if mod_rewriteis enabled on ApacheAND IISin PHP.

我想知道是否可以检查ApacheIISmod_rewrite是否启用了.PHP

ModRewritefor IIS exists. Check it here.

存在用于 IIS 的ModRewrite。检查它here

So, I'm looking for a PHP script that checks for mod_rewriteon Apacheand IIS.

所以,我在找一个PHP脚本,支票mod_rewrite的ApacheIIS。

Does anyone know such script or can write one?

有谁知道这样的脚本或可以写一个?

Especially for Microsoft IIS.

特别是对于 Microsoft IIS

Thanks!

谢谢!

采纳答案by kba

If you're using mod_php, you can use apache_get_modules(). This will return an array of all enabled modules, so to check if mod_rewriteis enabled, you could simply do

如果您使用的是 mod_php,则可以使用apache_get_modules(). 这将返回所有已启用模块的数组,因此要检查是否mod_rewrite已启用,您可以简单地执行

in_array('mod_rewrite', apache_get_modules());

Unfortunately, you're most likely trying to do this with CGI, which makes it a little bit more difficult.

不幸的是,您很可能会尝试使用 CGI 来执行此操作,这会增加一些难度。

You can test it using the following, though

不过,您可以使用以下方法对其进行测试

strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false

If the above condition evaluates to true, then mod_writeis enabled.

如果上述条件评估为true,则mod_write启用。

回答by Shankar Damodaran

Copy this piece of code and run it to find out.

复制这段代码并运行它以找出答案。



<?php
 if(!function_exists('apache_get_modules') ){ phpinfo(); exit; }
 $res = 'Module Unavailable';
 if(in_array('mod_rewrite',apache_get_modules())) 
 $res = 'Module Available';
?>
<html>
<head>
<title>A mod_rewrite availability check !</title></head>
<body>
<p><?php echo apache_get_version(),"</p><p>mod_rewrite $res"; ?></p>
</body>
</html>

回答by a.l.e

I like Christian Roy's solution:

我喜欢Christian Roy 的解决方案

###  .htaccess

<IfModule mod_rewrite.c>

    # Tell PHP that the mod_rewrite module is ENABLED.
    SetEnv HTTP_MOD_REWRITE On

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # The rest of your rewrite rules here

</IfModule>

Then, you can check in your PHP code for

然后,您可以检查您的 PHP 代码

    array_key_exists('HTTP_MOD_REWRITE', $_SERVER);

No idea if this works also with IIS (I have no way to check) but the odds are good.

不知道这是否也适用于 IIS(我无法检查),但可能性很大。

回答by Johnny Jensen

Upload a file called info.php with this code and run it:

使用以下代码上传一个名为 info.php 的文件并运行它:

<?php 
phpinfo();

Search for mod_rewrite on the page, and see if you can find it under Loaded Modules.

在页面上搜索 mod_rewrite,看看是否可以在 Loaded Modules 下找到它。

回答by loyola

don't make it so difficult you can simply find in phpinfo();

不要让它变得如此困难,你可以简单地找到 phpinfo();

enter image description here

在此处输入图片说明

Hope helpful!

希望有帮助!

Thanks

谢谢

回答by h0mayun

via command line we in centOs we can do this

通过命令行我们在centOs中我们可以做到这一点

httpd -l

回答by user1649798

<?php
phpinfo();
?>

Look under Configuration in the apache2handler in the Loaded Modules row.

在 Loaded Modules 行中查看 apache2handler 中的 Configuration。

This is simple and works.

这很简单而且有效。

<?php foreach( apache_get_modules() as $module ) echo "$module<br />";  ?>

回答by Ahmedul Haque Abid

This is my current method of checking if Mod_rewrite enabled for both Apache and IIS

这是我当前检查是否为 Apache 和 IIS 启用 Mod_rewrite 的方法

/**
 * --------------------------------------------------------------
 *  MOD REWRITE CHECK
 * --------------------------------------------------------------
 *                                        - By A H Abid
 * Define Constant for MOD REWRITE
 * 
 * Check if server allows MOD REWRITE. Checks for both 
 * Apache and IIS.
 * 
 */
if( function_exists('apache_get_modules') && in_array('mod_rewrite',apache_get_modules()) )
    $mod_rewrite = TRUE;
elseif( isset($_SERVER['IIS_UrlRewriteModule']) )
    $mod_rewrite = TRUE;
else
    $mod_rewrite = FALSE;
define('MOD_REWRITE', $mod_rewrite);

It works in my local machine and also worked in my IIS based webhost. However, on a particular apache server, it didn't worked for Apache as the apache_get_modules() was disabled but the mod_rewrite was enable in that server.

它适用于我的本地机器,也适用于我的基于 IIS 的网络主机。但是,在特定的 apache 服务器上,它不适用于 Apache,因为 apache_get_modules() 被禁用,但在该服务器中启用了 mod_rewrite。

回答by Amal Murali

Two lines of code:

两行代码:

$isEnabled = in_array('mod_rewrite', apache_get_modules());
echo ($isEnabled) ? 'Enabled' : 'Not enabled';

回答by TJHeuvel

You can get a list of installed apache modules, and check against that. Perhaps you can check if its installed by searching for its .dll (or linux equivalent) file.

您可以获得已安装apache 模块的列表,并进行检查。也许您可以通过搜索其 .dll(或 linux 等效文件)文件来检查它是否已安装。