php 如何检查服务器上是否启用了 mod_rewrite?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7337724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check whether mod_rewrite is enable on server?
提问by knightrider
Currently I am using the hosting with lightspeedserver. Hosting says mod_rewrite
is enabled but I can't get my script working there. Whenever I try to access the URL, it returns 404 - not foundpage.
目前我正在使用光速服务器的托管。托管说mod_rewrite
已启用,但我无法让我的脚本在那里工作。每当我尝试访问该 URL 时,它都会返回404 - 未找到页面。
I put the same codes at another server which is running with Apache. It's working over there. So I guess, it's the .htaccess
and mod_rewrite
issue.
我将相同的代码放在另一台运行 Apache 的服务器上。它在那里工作。所以我想,这是.htaccess
和mod_rewrite
问题。
But Hosting support is still insisting with me that their mod_rewrite is on, so I would like to know how can I check whether it's actually enabled or not.
但是托管支持仍然坚持我他们的 mod_rewrite 已启用,所以我想知道如何检查它是否实际启用。
I tried to check with phpinfo()
, but no luck, I can't find mod_rewrite
there, is it because they are using lightspeed
?
我试图检查phpinfo()
,但没有运气,我在mod_rewrite
那里找不到,是因为他们正在使用lightspeed
吗?
Is there any way to check? Please help me out. Thank you.
有什么办法检查吗?请帮帮我。谢谢你。
FYI:my .htaccess
code is
仅供参考:我的.htaccess
代码是
Options -Indexes
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteCond !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/ [L,QSA]
</IfModule>
I tried like this also
我也试过这样
DirectoryIndex index.php
RewriteEngine on
RewriteCond !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/ [L,QSA]
But same result.
但同样的结果。
回答by Fadzly Othman
To check if mod_rewrite module is enabled, create a new php file in your root folder of your WAMP server. Enter the following
phpinfo();
Access your created file from your browser.
CtrlFto open a search. Search for 'mod_rewrite'. If it is enabled you see it as 'Loaded Modules'
If not, open httpd.conf (Apache Config file) and look for the following line.
#LoadModule rewrite_module modules/mod_rewrite.so
Remove the pound ('#') sign at the start and save the this file.
Restart your apache server.
Access the same php file in your browser.
Search for 'mod_rewrite' again. You should be able to find it now.
要检查是否启用了 mod_rewrite 模块,请在 WAMP 服务器的根文件夹中创建一个新的 php 文件。输入以下内容
phpinfo();
从浏览器访问您创建的文件。
CtrlF打开搜索。搜索“mod_rewrite”。如果已启用,您会将其视为“已加载模块”
如果没有,请打开 httpd.conf(Apache 配置文件)并查找以下行。
#LoadModule rewrite_module modules/mod_rewrite.so
删除开头的井号 ('#') 并保存此文件。
重新启动您的 apache 服务器。
在浏览器中访问相同的 php 文件。
再次搜索“mod_rewrite”。您现在应该可以找到它。
回答by KawaiKx
from the command line, type
从命令行输入
sudo a2enmod rewrite
须藤 a2enmod 重写
if the rewrite mode is already enabled, it will tell you so!
如果重写模式已经启用,它会告诉你!
回答by Jahmic
If you are using a virtual hosts configuration file, make sure the virtual host in question has the directive AllowOverride All
somewhere like this:
如果您使用的是虚拟主机配置文件,请确保有问题的虚拟主机在AllowOverride All
某处具有如下指令:
<VirtualHost *:80>
...
<Directory "directory/of/your/.htaccess">
AllowOverride All
</Directory>
</VirtualHost>
回答by wappy
If apache_get_modules() is not recognized or no info about this module in phpinfo(); try to test mod rewrite by adding those lines in your .htaccess file:
如果 apache_get_modules() 无法识别或在 phpinfo() 中没有关于此模块的信息;尝试通过在 .htaccess 文件中添加这些行来测试 mod 重写:
RewriteEngine On
RewriteRule ^.*$ mod_rewrite.php
And mod_rewrite.php:
和 mod_rewrite.php:
<?php echo "Mod_rewrite is activated!"; ?>
回答by Lukas Lukac
console:
安慰:
<VirtualHost *:80>
...
<Directory ...>
AllowOverride All
</Directory>
</VirtualHost>
sudo a2enmod rewrite
sudo service apache2 restart
回答by Shivanshu
If
如果
in_array('mod_rewrite', apache_get_modules())
returns true
then mod-rewrite is enabled.
返回true
然后启用 mod-rewrite。
回答by starkeen
PHP's perdefined apache_get_modules()
function returns a list of enabled modules. To check if mod_rewrite
is enabled , you can run the following script on your server :
PHP 的 perdefinedapache_get_modules()
函数返回已启用模块的列表。要检查是否mod_rewrite
已启用,您可以在您的服务器上运行以下脚本:
<?php
print_r(apache_get_modules());
?>
If the above example fails, you can verify mod-rewrite using your .htaccess
file.
如果上述示例失败,您可以使用您的.htaccess
文件验证 mod-rewrite 。
Create an htaccess
file in the document root and add the following rewriteRule :
htaccess
在文档根目录中创建一个文件并添加以下 rewriteRule :
RewriteEngine on
RewriteRule ^helloWorld/?$ /index.php [NC,L]
Now visit http://example.com/HelloWorld, You will be internally forwarded to /index.phppage of your site. Otherwise, if mod-rewrite is disabled , you will get a 500 Internel server error.
现在访问http://example.com/HelloWorld,您将被内部转发到您网站的/index.php页面。否则,如果 mod-rewrite 被禁用,您将收到 500 内部服务器错误。
Hope this helps.
希望这可以帮助。
回答by Adi Prasetyo
回答by radtek
This works on CentOS:
这适用于 CentOS:
$ sudo httpd -M |grep rewrite_module
Should output rewrite_module (shared)
应该输出 rewrite_module (shared)
回答by Clive
If this code is in your .htaccess file (without the check for mod_rewrite.c)
如果此代码在您的 .htaccess 文件中(不检查 mod_rewrite.c)
DirectoryIndex index.php
RewriteEngine on
RewriteCond !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/ [L,QSA]
and you can visit any page on your site with getting a 500 server error I think it's safe to say mod rewrite is switched on.
并且您可以访问您网站上的任何页面,但出现 500 服务器错误 我认为可以安全地说 mod rewrite 已打开。