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

时间:2011-09-07 17:03:05

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

目前我正在使用 lightspeed 服务器托管。托管说mod_rewrite已启用,但我无法让我的脚本在那里工作。每当我尝试访问该网址时,都会返回 404 - 未找到页面。

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

但Hosting支持仍然坚持要求他们的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]

但结果相同。

17 个答案:

答案 0 :(得分:85)

  1. 要检查是否启用了mod_rewrite模块,请在WAMP服务器的根文件夹中创建一个新的php文件。输入以下

    phpinfo();

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

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

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

    #LoadModule rewrite_module modules/mod_rewrite.so

  5. 在开始时删除井号('#')并保存此文件。

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

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

  8. 再次搜索“mod_rewrite”。你现在应该能够找到它。

答案 1 :(得分:64)

从命令行

键入

  

sudo a2enmod重写

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

答案 2 :(得分:51)

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

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

答案 3 :(得分:15)

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

RewriteEngine On
RewriteRule ^.*$ mod_rewrite.php

和mod_rewrite.php:

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

答案 4 :(得分:12)

console:

<VirtualHost *:80>
        ...
    <Directory ...>
        AllowOverride All
    </Directory>
</VirtualHost>

sudo a2enmod rewrite
sudo service apache2 restart

答案 5 :(得分:11)

如果

in_array('mod_rewrite', apache_get_modules())

返回true,然后启用mod-rewrite。

答案 6 :(得分:9)

你可以在终端上做到:

apachectl -M
apache2ctl -M

取自2daygeek

答案 7 :(得分:8)

如果此代码在.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重写已经开启。

答案 8 :(得分: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://bootboxjs.com/documentation.html#bb-alert-dialog,您将在内部转发到您网站的 /index.php 页面。否则,如果禁用mod-rewrite,您将收到500 Internel服务器错误。

希望这会有所帮助。

答案 9 :(得分:5)

这适用于CentOS:

$ sudo httpd -M |grep rewrite_module

应输出rewrite_module (shared)

答案 10 :(得分:4)

您可以使用php功能

      apache_get_modules

并检查mod_rewrite

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

http://in2.php.net/apache_get_modules

答案 11 :(得分:4)

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

答案 12 :(得分:3)

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

 <?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服务器图标 - &gt; Apache - &gt; Apache模块并检查重写模块选项

答案 13 :(得分:1)

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

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

答案 14 :(得分:0)

您只需输入

即可检查文件是否存在

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

结果行可能不会以#

开头发表评论

答案 15 :(得分:0)

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

希望这可以节省一些人我所经历的压力,找到它的错误。

答案 16 :(得分:0)

此代码对我有用:

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