如何检查服务器是否启用了mod_rewrite?

134

目前我正在使用安装了Lightspeed服务器的主机。主机商称已启用mod_rewrite,但我无法在该服务器上运行我的脚本。每当我尝试访问URL时,都会返回404-未找到页面。

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

但是主机支持仍然坚称他们的mod_rewrite已经启用,所以我想知道如何检查它是否真正启用。

我尝试使用phpinfo()来检查,但没有成功,在那里我找不到mod_rewrite,这是因为他们正在使用Lightspeed吗?

有没有办法检查?请帮帮我。谢谢。

FYI: 我的.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]

但是结果相同。


1
验证mod-rewrite是否启用的最简单方法是将一些随机文本粘贴到您的htaccess文件中,然后访问您的网站,如果该模块已启用,则会出现内部服务器错误。这是我几个月前写的一个快速的“htaccess入门教程”,https://helponnet.com/2021/04/15/htaccess-tutorial-for-beginers/ 希望您会发现它有用。 - Amit Verma
还可以阅读“如何检查htaccess是否有效”https://helponnet.com/2021/05/14/know-wether-htaccess-is-working-on-apache/ - Amit Verma
19个回答

5
我知道这个问题有点老了,但是如果你可以编辑你的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>

最好告诉需要更改的文件的确切名称和可能的位置,这对新手很有帮助。谢谢。 - MindRoasterMir
还需注意的是,更改只有在重新启动Apache后才会生效。谢谢。 - MindRoasterMir

5

2
apache_get_modules() 只有在 PHP 作为 Apache 模块运行时才能工作。OP 表示 PHP 正在运行在 Lightspeed 上。 - MrWhite

5

如果您在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


4

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

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

运行此页面,然后查找将能知道模块是否可用,如果不可用,则可以向您的托管提供商询问,或者如果您想在本地机器上启用它,则可以检查此与启用WAMP Apache中的重写模块相关的逐步教程https://youtu.be/xIspOX9FuVU?t=1m43s。单击Wamp服务器图标 -> Apache -> Apache模块并检查重写模块选项。


4
这段代码对我很有效:
if (strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false) echo "mod_rewrite enabled";
else echo "mod_rewrite disabled";

3

首先您需要检查模块是否已安装

apachectl -M

如果它没有显示在列表上,你必须激活它:

a2enmod rewrite

现在,重新启动您的服务器并进行测试。

systemctl restart apache2

1
您只需要检查文件是否存在,输入以下命令即可:cat /etc/apache2/mods-available/rewrite.load。结果行可能没有以#开头的注释。

1

在根目录下创建一个php文件,然后添加以下代码

<?php
if (in_array('mod_rewrite', apache_get_modules())) {
    echo "mod_rewrite is enabled";
} else {
    echo "mod_rewrite is not enabled";
}

0

我曾经遇到过同样的问题,后来通过点击自定义结构,添加/index.php/%postname%/解决了它。

希望这能帮助其他人节省找错问题的时间和精力。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接