apache mod_rewrite 不工作或未启用
我已经在 Apache 上安装了 rewrite_module 并修改了 php.ini
。
我创建了 rewrite.php
和 .htaccess
文件,但它不起作用。
我的文件系统文件夹如:
/var/www/html
/var/www/html/test
/var/www/html/test/.htaccess
/var/www/html/test/rewrite.php
/var/www/html/.htaccess 内容是:
$ cat /var/www/html/test/.htaccess
RewriteEngine On
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
/var/www/html/rewrite.php
$ cat /var/www/html/test/rewrite.php
<html>
<h2 align=center>
<?php
// mod_rewrite Test Page
// Copyright 2006 Webune.com
if($_GET['link']==1){echo"You are not using mod_rewrite";}
elseif($_GET['link']==2){echo"Congratulations!! You are using Apache mod_rewrite";}
else{echo"Linux Apache mod_rewrte Test Tutorial";}
?>
</h2>
<hr>
<head>
<title>How To Test mod_rewrite in Apache Linux Server</title>
</head>
<body>
<p align="center">by <a href="http://www.webune.com">Webune</a></p>
<p><a href="rewrite.php?link=1">LINK1</a> = rewrite.php?link=1</p>
<p><a href="link2.html">LINK2</a> = link2.html</p>
<p>How this works: both links are for this same page, except they both are different. link one is without the mod_rewrite and link2 is using mod_rewrite. Link1 show the php file, with with mod_rewrite we are mascarading the php file into a html file. you can use whatever type of extension you want, you can change it to .htm or .shtml etc... all you have to do is to make sure you also chang it in the .htaccess file</p>
<p><< <a href="http://www.webune.com/forums/viewtopic-p-62.html">Go back to webune forums.</a></p>
</body>
</html>
mod_rewrite.so 已安装。
$ ls -l mod_rewrite.so
-rwxr-xr-x 1 root root 59256 Sep 20 23:34 mod_rewrite.so
rewrite_module 已加载。
$ cat /etc/httpd/conf/httpd.conf | grep mod_rewrite.so
LoadModule rewrite_module modules/mod_rewrite.so
允许覆盖设置
$ cat /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
I have installed rewrite_module and modified php.ini
on Apache.
I create rewrite.php
and .htaccess
files, but it's not working.
My filesystem folders like:
/var/www/html
/var/www/html/test
/var/www/html/test/.htaccess
/var/www/html/test/rewrite.php
/var/www/html/.htaccess content is:
$ cat /var/www/html/test/.htaccess
RewriteEngine On
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
/var/www/html/rewrite.php
$ cat /var/www/html/test/rewrite.php
<html>
<h2 align=center>
<?php
// mod_rewrite Test Page
// Copyright 2006 Webune.com
if($_GET['link']==1){echo"You are not using mod_rewrite";}
elseif($_GET['link']==2){echo"Congratulations!! You are using Apache mod_rewrite";}
else{echo"Linux Apache mod_rewrte Test Tutorial";}
?>
</h2>
<hr>
<head>
<title>How To Test mod_rewrite in Apache Linux Server</title>
</head>
<body>
<p align="center">by <a href="http://www.webune.com">Webune</a></p>
<p><a href="rewrite.php?link=1">LINK1</a> = rewrite.php?link=1</p>
<p><a href="link2.html">LINK2</a> = link2.html</p>
<p>How this works: both links are for this same page, except they both are different. link one is without the mod_rewrite and link2 is using mod_rewrite. Link1 show the php file, with with mod_rewrite we are mascarading the php file into a html file. you can use whatever type of extension you want, you can change it to .htm or .shtml etc... all you have to do is to make sure you also chang it in the .htaccess file</p>
<p><< <a href="http://www.webune.com/forums/viewtopic-p-62.html">Go back to webune forums.</a></p>
</body>
</html>
mod_rewrite.so is installed.
$ ls -l mod_rewrite.so
-rwxr-xr-x 1 root root 59256 Sep 20 23:34 mod_rewrite.so
rewrite_module is loaded.
$ cat /etc/httpd/conf/httpd.conf | grep mod_rewrite.so
LoadModule rewrite_module modules/mod_rewrite.so
AllowOverride setting
$ cat /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它正在发挥作用。
我的解决方案是:
1.在 /etc/httpd/conf.d/test.conf 中创建一个 test.conf
2.编写一些规则,例如:
3.重新启动 Apache 服务器。
4.自己再试一次。
It's working.
my solution is:
1.create a test.conf into /etc/httpd/conf.d/test.conf
2.wrote some rule, like:
3.restart your Apache server.
4.try again yourself.
为了让 mod_rewrite 在 Apache 2.4 中为我工作,我必须在下面添加“要求所有已授予”行。
据说 Apache 2.2 也存在类似的要求,如果您正在使用它:
请注意
ErrorDocument 404
指令有时也可以覆盖这些内容,因此如果它不起作用,请尝试注释掉您的 ErrorDocument 指令看看它是否有效。上面的示例可用于通过在路径中包含子文件夹来确保站点不会从缓存中提供服务,尽管文件驻留在服务器的根目录中。To get mod_rewrite to work for me in Apache 2.4, I had to add the "Require all granted" line below.
supposedly a similar requirement exists for Apache 2.2 as well, if you're using that:
Note that an
ErrorDocument 404
directive can sometimes override these things as well, so if it's not working try commenting out your ErrorDocument directive and see if it works. The above example can be used to ensure a site isn't served from cache by including a subfolder in the path, though the files reside at the root of the server.在centOS7上我更改了文件
/etc/httpd/conf/httpd.conf
来自AllowOverride 无
允许覆盖全部
On centOS7 I changed the file
/etc/httpd/conf/httpd.conf
from AllowOverride None
to AllowOverride All
我终于通过简单地使用 docker 文件中的 RUN 和 CMD 命令在 docker-compose 文件的初始启动/运行中完成了这项工作。启用 mod 重写后我必须重新启动 Apache。
我的 .htaccess 文件中也有以下内容,该文件位于我的 php 服务器根目录中。
I finally got this working on the initial startup/run of the docker-compose file by simply using the RUN and CMD commands inside the docker file. I have to restart Apache after enabling the mod rewrite.
I also have the following in my .htaccess file that is in the root of my php server root.
尝试设置:“AllowOverride All”。
Try setting: "AllowOverride All".
请尝试
或使用正确的apache重启命令
Please try
or use correct apache restart command