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

kni*_*der 95 php .htaccess mod-rewrite url-rewriting lightspeed

目前我正在使用lightspeed服务器托管.托管说mod_rewrite已启用但我无法让我的脚本在那里工作.每当我尝试访问该URL时,它都会返回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>
Run Code Online (Sandbox Code Playgroud)

我也尝试过这样

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]
Run Code Online (Sandbox Code Playgroud)

但结果相同.

小智 89

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

    phpinfo();

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

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

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

    #LoadModule rewrite_module modules/mod_rewrite.so

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

  6. 重启你的apache服务器.

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

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

  • 我相信当PHP本身作为Apache模块运行时,phpinfo()会报告_Apache_ Loaded Modules.OP声明PHP正在Lightspeed上运行,Lightspeed有自己的mod_rewrite兼容重写引擎. (8认同)
  • 请注意,如果您将PHP作为CGI应用程序运行,则此方法将不起作用(如果`phpinfo()的"服务器API"字段显示"CGI/FastCGI",则会出现这种情况).`phpinfo()`不会列出启用的模块.在这种情况下,请参阅[如何在PHP CGI上检查mod_rewrite](/sf/ask/1281712841/). (4认同)
  • 我必须输入第一步:<?php echo phpinfo(); ?> (2认同)

Kaw*_*iKx 81

从命令行输入

sudo a2enmod重写

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

  • 是的!完美,完美. (2认同)

Jah*_*mic 58

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

<VirtualHost *:80>
        ...
    <Directory "directory/of/your/.htaccess">
        AllowOverride All
    </Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这是实际解决了我的问题:) (5认同)
  • 谢谢.我欠你一个百威啤酒.<3 (2认同)
  • 理查德如果这个答案对您有用并解决了您的问题,您应该将其标记为正确并奖励@Jahmic 他应得的。 (2认同)

wap*_*ppy 16

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

RewriteEngine On
RewriteRule ^.*$ mod_rewrite.php
Run Code Online (Sandbox Code Playgroud)

和mod_rewrite.php:

<?php echo "Mod_rewrite is activated!"; ?>
Run Code Online (Sandbox Code Playgroud)


Blo*_*kas 14

安慰:

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

sudo a2enmod rewrite
sudo service apache2 restart
Run Code Online (Sandbox Code Playgroud)


Shi*_*shu 11

如果

in_array('mod_rewrite', apache_get_modules())
Run Code Online (Sandbox Code Playgroud)

返回true然后启用mod-rewrite.


sta*_*een 10

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

<?php
print_r(apache_get_modules());
?>
Run Code Online (Sandbox Code Playgroud)

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

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

RewriteEngine on

RewriteRule ^helloWorld/?$ /index.php [NC,L]
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助.


Adi*_*tyo 9

你可以在终端上做到:

apachectl -M
apache2ctl -M
Run Code Online (Sandbox Code Playgroud)

取自2daygeek


Cli*_*ive 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]
Run Code Online (Sandbox Code Playgroud)

并且您可以访问您网站上的任何页面并获得500服务器错误我认为可以安全地说mod重写已打开.

  • 那么你可以访问网站上的一些页面而不会出现500错误?这通常意味着如果正在读取.htaccess文件,则必须打开mod_rewrite.但是.htaccess可能没有被读取...尝试在.htaccess文件的顶部写一些无意义的字符,如果Apache实际上正在读取文件,这将导致连接因500服务器错误而死亡.如果正在阅读,您能否举例说明您认为应该有效的URL,但不是吗? (3认同)

rad*_*tek 6

这适用于CentOS:

$ sudo httpd -M |grep rewrite_module
Run Code Online (Sandbox Code Playgroud)

应输出 rewrite_module (shared)


Miq*_*Ali 5

你可以使用php函数

      apache_get_modules
Run Code Online (Sandbox Code Playgroud)

并检查 mod_rewrite

<pre>
<?php
print_r(apache_get_modules());
?>
</pre>
Run Code Online (Sandbox Code Playgroud)

http://in2.php.net/apache_get_modules

  • apache_get_modules() 仅在 PHP 作为 Apache 模块运行时才有效。OP 声明 PHP 正在 Lightspeed 上运行。 (2认同)

Eze*_*one 5

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

cd /etc/apache2/mods-available
Run Code Online (Sandbox Code Playgroud)

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

cd /etc/php/7.0/mods-available
Run Code Online (Sandbox Code Playgroud)

输入: ll -a


小智 5

我知道这个问题已经很老了,但是如果您可以将 Apache 配置文件编辑AllowOverride AllAllowOverride 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>
Run Code Online (Sandbox Code Playgroud)