Php 如何检查服务器上是否启用mod_rewrite?
目前我正在使用lightspeed服务器的主机。主机说,Php 如何检查服务器上是否启用mod_rewrite?,php,.htaccess,mod-rewrite,url-rewriting,lightspeed,Php,.htaccess,Mod Rewrite,Url Rewriting,Lightspeed,目前我正在使用lightspeed服务器的主机。主机说,mod_rewrite已启用,但我的脚本无法在那里运行。每当我尝试访问URL时,它都会返回404-notfound页面 <pre> <?php print_r(apache_get_modules()); ?> </pre> 我将相同的代码放在另一台使用Apache运行的服务器上。它在那边工作。所以我想,这是.htaccess和mod_rewrite问题 但是主机支持人员仍然坚持要求我打开他们的mod_
mod_rewrite
已启用,但我的脚本无法在那里运行。每当我尝试访问URL时,它都会返回404-notfound页面
<pre>
<?php
print_r(apache_get_modules());
?>
</pre>
我将相同的代码放在另一台使用Apache运行的服务器上。它在那边工作。所以我想,这是.htaccess
和mod_rewrite
问题
但是主机支持人员仍然坚持要求我打开他们的mod_重写,所以我想知道如何检查它是否实际启用
我试着用phpinfo()
检查,但是运气不好,我在那里找不到mod\u 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>
但结果相同。如果此代码在.htaccess文件中(不检查mod_rewrite.c) 你可以访问你网站上的任何一个页面,出现500个服务器错误。我认为可以肯定的说,mod rewrite已经打开了
phpinfo()代码>
\LoadModule rewrite\u module modules/mod\u rewrite.so
您可以使用php函数
apache_get_modules
并检查mod_重写
如果您使用的是虚拟主机配置文件,请确保相关虚拟主机具有指令
AllowOverride All
,如下所示:
<VirtualHost *:80>
...
<Directory ...>
AllowOverride All
</Directory>
</VirtualHost>
sudo a2enmod rewrite
sudo service apache2 restart
...
允许超越所有
基本上,这表示允许处理所有.htaccess指令。控制台:
in_array('mod_rewrite', apache_get_modules())
...
允许超越所有
sudoa2enmod重写
sudo服务apache2重启
如果
返回true
则启用mod rewrite。如果无法识别apache_get_modules()或phpinfo()中没有关于此模块的信息;尝试通过在.htaccess文件中添加以下行来测试mod rewrite:
<?php echo "Mod_rewrite is activated!"; ?>
和mod_rewrite.php:
<?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>
从命令行键入 sudoa2enmod重写
如果重写模式已经启用,它会告诉您是这样的 只需创建一个新页面并添加此代码即可
cd /etc/apache2/mods-available
mod_重写可用性检查!
并运行此页面,然后查找将能够知道模块是否可用,如果不可用,则您可以询问您的主机,或者如果您希望在本地计算机中启用该模块,然后检查此youtube分步教程,该教程与在wamp apache中启用重写模块相关
Wamp server icon->Apache->Apache Modules并检查重写模块选项如果您在linux系统中,您可以在以下文件夹中检查apache2的所有启用模块:/etc/apache2/mods available
cd /etc/php/7.0/mods-available
$ sudo httpd -M |grep rewrite_module
键入:ll-a
如果您想检查php的可用模块(在本例中为PHP7) 文件夹/etc/php/7.0/mods可用
cd /etc/php/7.0/mods-available
$ sudo httpd -M |grep rewrite_module
键入:ll-a
这在CentOS上有效:
<?php
print_r(apache_get_modules());
?>
应输出
rewrite\u模块(共享)
PHP的perdefinedapache\u get\u modules()
函数返回已启用模块的列表。要检查是否启用了mod_rewrite
,可以在服务器上运行以下脚本:
RewriteEngine on
RewriteRule ^helloWorld/?$ /index.php [NC,L]
现在访问,您将被内部转发到站点的/index.php页面。否则,如果mod rewrite被禁用,您将获得500 Internel服务器错误
希望这有帮助 您可以在终端上执行此操作,或者:
if (strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false) echo "mod_rewrite enabled";
else echo "mod_rewrite disabled";
摘自您只需通过键入来检查文件是否存在
cat/etc/apache2/mods可用/重写。加载
结果行可能不会以
#
开头进行注释。我遇到了确切的问题,我通过单击“自定义结构”解决了它,然后添加/index.php/%postname%/
,它可以正常工作
希望这能帮别人减轻我发现它到底出了什么问题时的压力。这段代码对我很有用:
<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>
我知道这个问题很老,但是如果您可以从
AllowOverride None
#
#选项指令的可能值为“无”、“全部”,
#或以下任何组合:
#索引包括以下符号链接符号链接所有者匹配执行CGI多视图
#
#请注意,“多视图”必须显式命名为*--“所有选项”
#不给你。
#
#期权指令既复杂又重要。请看
# http://httpd.apache.org/docs/2.4/mod/core.html#options
#了解更多信息。
#
选项索引跟随符号链接
#
#AllowOverride控制可在.htaccess文件中放置的指令。
#它可以是“全部”、“无”或关键字的任意组合:
#AllowOverride文件信息AuthConfig限制
#
允许超越所有
#
#控制谁可以从此服务器获取内容。
#
要求所有授权
这样您就可以访问网站上的某些页面而不会出现错误?这通常意味着,如果正在读取.htaccess文件,则必须打开mod_rewrite。但是,可能没有读取.htaccess…请尝试在.htac顶部写入一些无意义的字符
apachectl -M
apache2ctl -M
if (strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false) echo "mod_rewrite enabled";
else echo "mod_rewrite disabled";
<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>