目前,我正在将主机与lightspeed服务器一起使用。托管说mod_rewrite
已启用,但我无法在那里运行脚本。每当我尝试访问URL时,它都会返回404-找不到页面。
我将相同的代码放在另一台运行Apache的服务器上。它在那边工作。所以我想这是.htaccess
和mod_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]
但结果相同。
在命令行中,键入
sudo a2enmod重写
如果已经启用了重写模式,它将告诉您!
-
要检查是否启用了mod_rewrite模块,请在WAMP服务器的根文件夹中创建一个新的php文件。输入以下内容
phpinfo();
-
从浏览器访问您创建的文件。
-
CtrlF打开搜索。搜索“ mod_rewrite”。如果启用,则将其视为“已加载的模块”
-
如果没有,请打开httpd.conf(Apache Config文件)并查找以下行。
#LoadModule rewrite_module modules/mod_rewrite.so
-
删除开头的井号('#')并保存此文件。
-
重新启动您的apache服务器。
-
在浏览器中访问相同的php文件。
-
再次搜索“ mod_rewrite”。您现在应该可以找到它。
如果您使用的是虚拟主机配置文件,请确保所讨论的虚拟主机的指令AllowOverride All
如下所示:
<VirtualHost *:80>
...
<Directory "directory/of/your/.htaccess">
AllowOverride All
</Directory>
</VirtualHost>
基本上,这表示允许处理所有.htaccess指令。
如果apache_get_modules()无法识别或phpinfo()中没有有关此模块的信息;尝试通过在.htaccess文件中添加以下行来测试mod重写:
RewriteEngine On
RewriteRule ^.*$ mod_rewrite.php
和mod_rewrite.php:
<?php echo "Mod_rewrite is activated!"; ?>
安慰:
<VirtualHost *:80>
...
<Directory ...>
AllowOverride All
</Directory>
</VirtualHost>
sudo a2enmod rewrite
sudo service apache2 restart
如果
in_array('mod_rewrite', apache_get_modules())
返回,true
然后启用mod-rewrite。
PHP的perdefinedapache_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 Internel服务器错误。
希望这可以帮助。
您可以在终端上执行以下任一操作:
apachectl -M
apache2ctl -M
取自2daygeek
这适用于CentOS:
$ sudo httpd -M |grep rewrite_module
应该输出 rewrite_module (shared)
如果此代码位于您的.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已打开。
您可以使用php函数
apache_get_modules
并检查是否有mod_rewrite
<pre>
<?php
print_r(apache_get_modules());
?>
</pre>
http://in2.php.net/apache_get_modules
如果您使用的是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
只需创建一个新页面并添加此代码
<?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逐步教程,以在wamp apache中启用重写模块
https://youtu.be/xIspOX9FuVU?t=1m43s
Wamp服务器图标-> Apache-> Apache Modules,然后检查重写模块选项
我知道这个问题很旧,但是如果您可以将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>
您只需输入以下内容来检查文件是否存在:
cat /etc/apache2/mods-available/rewrite.load
结果行可能不会以开头 #
这段代码对我有用:
if (strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false) echo "mod_rewrite enabled";
else echo "mod_rewrite disabled";
我遇到了确切的问题,我通过单击自定义结构,然后添加/index.php/%postname%/
并解决了问题
希望这可以减轻某人的压力,而这正是我经历的麻烦所在。
文章标签:.htaccess , lightspeed , mod-rewrite , php , url-rewriting
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!