【问题标题】:How to check whether mod_rewrite is enable on server?如何检查服务器上是否启用了 mod_rewrite?
【发布时间】:2011-11-12 08:33:20
【问题描述】:

目前我正在使用带有 lightspeed 服务器的主机。托管说mod_rewrite 已启用,但我无法让我的脚本在那里工作。每当我尝试访问该 URL 时,它都会返回 404 - not found 页面。

我将相同的代码放在另一台运行 Apache 的服务器上。它在那里工作。所以我猜,这是.htaccessmod_rewrite 的问题。

但是托管支持仍然坚持告诉我他们的 mod_rewrite 已打开,所以我想知道如何检查它是否实际启用。

我尝试与phpinfo() 联系,但没有运气,我在那里找不到mod_rewrite,是因为他们使用的是lightspeed吗?

有什么方法可以检查吗?请帮帮我。谢谢。

仅供参考:我的.htaccess 代码是

Options -Indexes

<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on

RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>

我也试过这样

DirectoryIndex index.php
RewriteEngine on

RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

但结果相同。

【问题讨论】:

标签: php .htaccess mod-rewrite url-rewriting lightspeed


【解决方案1】:

从命令行输入

sudo a2enmod 重写

如果重写模式已经开启,它会告诉你!

【讨论】:

    【解决方案2】:
    1. 要检查是否启用了 mod_rewrite 模块,请在 WAMP 服务器的根文件夹中创建一个新的 php 文件。输入以下内容

      phpinfo();

    2. 从浏览器访问您创建的文件。

    3. CtrlF 打开搜索。搜索“mod_rewrite”。如果已启用,您会将其视为“已加载模块”

    4. 如果没有,请打开 httpd.conf(Apache 配置文件)并查找以下行。

      #LoadModule rewrite_module modules/mod_rewrite.so

    5. 删除开头的井号 ('#') 并保存此文件。

    6. 重新启动您的 apache 服务器。

    7. 在浏览器中访问相同的 php 文件。

    8. 再次搜索“mod_rewrite”。现在应该可以找到了。

    【讨论】:

    • 我相信当 PHP 本身作为 Apache 模块运行时,phpinfo() 会报告 Apache 加载的模块。 OP 声明 PHP 在 Lightspeed 上运行,它有自己的 mod_rewrite 兼容重写引擎。
    • 第一步我必须输入:
    • 请注意,如果您将 PHP 作为 CGI 应用程序运行(如果phpinfo() 的“服务器 API”字段显示“CGI/FastCGI”,则会出现这种情况),此方法将不起作用。 phpinfo() 不会列出启用的模块。在这种情况下,请参阅How to check for mod_rewrite on PHP CGI
    【解决方案3】:

    如果您使用的是虚拟主机配置文件,请确保有问题的虚拟主机具有指令 AllowOverride All,如下所示:

    <VirtualHost *:80>
            ...
        <Directory "directory/of/your/.htaccess">
            AllowOverride All
        </Directory>
    </VirtualHost>
    

    基本上,这表明允许处理所有 .htaccess 指令。

    【讨论】:

    • 谢谢。我欠你一个百威。
    • ... 中的 &lt;Directory ...&gt; 是什么意思?我应该放什么? .htaccess 所在的目录?
    • 是的,...应该在引号之间提及“...” .htaccess 所在的目录
    • 它有。我从服务器复制了 conf 文件,虽然我更改了 ServerName,但我没有更改目录名称,然后重新启动 apache 就可以了。
    • Richard如果这个答案对您有用并解决了您的问题,您应该将其标记为正确并奖励@Jahmic 他应得的。
    【解决方案4】:

    这适用于 CentOS:

    $ sudo httpd -M |grep rewrite_module
    

    应该输出rewrite_module (shared)

    【讨论】:

    • 这是在 CentOS 上最好的方法,假设你有 ssh 访问权限。谢谢,@radtek!
    【解决方案5】:

    如果 apache_get_modules() 无法识别或 phpinfo() 中没有关于此模块的信息;尝试通过在 .htaccess 文件中添加这些行来测试 mod 重写:

    RewriteEngine On
    RewriteRule ^.*$ mod_rewrite.php
    

    还有 mod_rewrite.php:

    <?php echo "Mod_rewrite is activated!"; ?>
    

    【讨论】:

      【解决方案6】:

      控制台:

      <VirtualHost *:80>
              ...
          <Directory ...>
              AllowOverride All
          </Directory>
      </VirtualHost>
      
      sudo a2enmod rewrite
      sudo service apache2 restart
      

      【讨论】:

      • 我已经尝试了很多来解决这个问题,然后在尝试这个时它对我有用。我现在有最后一个 apache 和 ubuntu。
      【解决方案7】:

      如果

      in_array('mod_rewrite', apache_get_modules())
      

      返回 true 然后启用 mod-rewrite。

      【讨论】:

      • 完美的一站式解决方案,通过 PHP 代码检查重写模块是否启用。
      【解决方案8】:

      PHP 的 perdefined apache_get_modules() 函数返回启用模块的列表。要检查mod_rewrite 是否启用,您可以在服务器上运行以下脚本:

      <?php
      print_r(apache_get_modules());
      ?>
      

      如果上述示例失败,您可以使用 .htaccess 文件验证 mod-rewrite。

      在文档根目录中创建一个htaccess 文件并添加以下 rewriteRule :

      RewriteEngine on
      
      RewriteRule ^helloWorld/?$ /index.php [NC,L]
      

      现在访问http://example.com/HelloWorld,您将被内部转发到您网站的/index.php页面。否则,如果 mod-rewrite 被禁用,您将收到 500 内部服务器错误。

      希望这会有所帮助。

      【讨论】:

      • 我使用 7.2,apache_get_modules 未定义。编辑.htaccess后是否必须重新启动apache2?
      • 不,使用 htaccess 时不需要重新启动服务器。
      【解决方案9】:

      你可以在终端上做:

      apachectl -M
      apache2ctl -M
      

      取自2daygeek

      【讨论】:

      • 在 Debian 9 中,它确实有效,但您必须以 root 身份登录或使用 sudo
      【解决方案10】:

      如果此代码在您的 .htaccess 文件中(不检查 mod_rewrite.c)

      DirectoryIndex index.php
      RewriteEngine on
      
      RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
      

      并且您可以访问您网站上的任何页面并收到 500 服务器错误我认为可以肯定地说 mod rewrite 已打开。

      【讨论】:

      • 所以您可以访问网站上的某些页面而不会出现 500 错误?这通常意味着如果正在读取 .htaccess 文件,则必须打开 mod_rewrite。但是 .htaccess 可能没有被读取...尝试在 .htaccess 文件的顶部写入一些无意义的字符,如果 Apache 实际正在读取文件,这将导致连接因 500 服务器错误而终止。如果正在阅读,您能否举一个您认为应该有效但无效的 URL 示例?
      • 问题是,它不是 apache 服务器,它是 lightspeed。我从来没有收到 500 错误,我得到的只是 404 错误。这就是为什么我怀疑 .htaccess 没有启用。
      • 很可能是的,我会尝试将无意义的字符放入 .htaccess 中,看看当您直接进入非重写页面(例如 index.php)时是否收到 500 错误。如果没有,可能值得发布一个新问题,询问如何使用 Lightspeed 启用 .htaccess
      【解决方案11】:

      可以使用php函数

            apache_get_modules
      

      并检查 mod_rewrite

      <pre>
      <?php
      print_r(apache_get_modules());
      ?>
      </pre>
      

      http://in2.php.net/apache_get_modules

      【讨论】:

      • apache_get_modules() 仅在 PHP 作为 Apache 模块运行时才有效。 OP 声明 PHP 正在 Lightspeed 上运行。
      【解决方案12】:

      如果您在 linux 系统中,您可以在以下文件夹中检查 apache2(在我的情况下)的所有启用模块:/etc/apache2/mods-available

      cd /etc/apache2/mods-available
      

      输入:ll -a
      如果您想检查 php 的可用模块(在本例中为 php 7 ) 文件夹 /etc/php/7.0/mods-available

      cd /etc/php/7.0/mods-available
      

      输入:ll -a

      【讨论】:

        【解决方案13】:

        只需创建一个新页面并添加此代码

         <?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>
        

        并运行此页面,然后查找将能够知道模块是否可用,如果没有,那么您可以询问您的主机,或者如果您想在本地机器上启用它,然后查看这个与启用重写模块相关的 youtube 分步教程在沼泽阿帕奇 https://youtu.be/xIspOX9FuVU?t=1m43s Wamp 服务器图标 -> Apache -> Apache Modules 并检查 rewrite module 选项

        【讨论】:

          【解决方案14】:

          我知道这个问题很老,但是如果您可以将 Apache 配置文件从 AllowOverride None 编辑为 AllowOverride All

          <Directory "${SRVROOT}/htdocs">
              #
              # Possible values for the Options directive are "None", "All",
              # or any combination of:
              #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
              #
              # Note that "MultiViews" must be named *explicitly* --- "Options All"
              # doesn't give it to you.
              #
              # The Options directive is both complicated and important.  Please see
              # http://httpd.apache.org/docs/2.4/mod/core.html#options
              # for more information.
              #
              Options Indexes FollowSymLinks
          
              #
              # AllowOverride controls what directives may be placed in .htaccess files.
              # It can be "All", "None", or any combination of the keywords:
              #   AllowOverride FileInfo AuthConfig Limit
              #
              AllowOverride All
          
              #
              # Controls who can get stuff from this server.
              #
              Require all granted
          </Directory>
          

          【讨论】:

          • 最好告知确切的名称,并在可能的情况下告知需要更改的文件的位置。对新人会有帮助。谢谢
          • 还应该记住,更改只有在您重新启动 apache 后才会生效。谢谢
          【解决方案15】:

          这段代码对我有用:

          if (strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false) echo "mod_rewrite enabled";
          else echo "mod_rewrite disabled";
          

          【讨论】:

            【解决方案16】:

            你只需要通过输入来检查文件是否存在

            cat /etc/apache2/mods-available/rewrite.load

            #开头的结果行不能被注释

            【讨论】:

              【解决方案17】:

              首先检查模块是否安装

              apachectl -M
              

              它没有显示在列表中,你必须激活它:

              a2enmod rewrite
              

              现在,重启你的服务器并测试

              systemctl restart apache2
              

              【讨论】:

                【解决方案18】:

                我遇到了确切的问题,我通过单击自定义结构解决了它,然后添加 /index.php/%postname%/ 并且它可以工作

                希望这可以减轻我在发现问题所在时所承受的压力。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2014-08-22
                  • 2012-03-13
                  • 1970-01-01
                  • 2016-04-24
                  • 1970-01-01
                  • 2011-01-19
                  相关资源
                  最近更新 更多