apache mod_rewrite不工作或未启用

9rbhqvlz  于 2023-04-21  发布在  Apache
关注(0)|答案(6)|浏览(223)

我已经在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>&lt;&lt; <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

AllowOverride设置

$ cat /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny
    Allow from all
</Directory>
tyu7yeag

tyu7yeag1#

尝试设置:“AllowOverride All”。

falq053o

falq053o2#

请试试

sudo a2enmod rewrite

或使用正确的apache restart命令

sudo /etc/init.d/apache2 restart
brccelvz

brccelvz3#

有效果了
我的解决方案是:
1.在/etc/httpd/conf.d/test.conf中创建test.conf
2.写了一些规则,比如:

<Directory "/var/www/html/test">
RewriteEngine On
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
</Directory>

3.重启Apache服务器
4.你自己再试试。

a7qyws3x

a7qyws3x4#

为了让mod_rewrite在Apache 2.4中为我工作,我必须在下面添加“Require all granted”行。

<Directory /var/www>
   # Required if running apache > 2.4
   Require all granted

   RewriteEngine on 
   RewriteRule ^cachebust-([a-z0-9]+)\/(.*) /$2 [L] 
 </Directory>

假设Apache 2.2也存在类似的需求,如果你使用的是:

<Directory /var/www>
   # Required if running apache 2.2
   Order allow,deny
   Allow from all

   RewriteEngine on 
   RewriteRule ^cachebust-([a-z0-9]+)\/(.*) /$2 [L] 
 </Directory>

请注意,ErrorDocument 404指令有时也会覆盖这些内容,所以如果它不起作用,请尝试注解掉ErrorDocument指令,看看它是否起作用。上面的例子可以用来确保网站不从缓存中提供服务,方法是在路径中包含子文件夹,尽管文件位于服务器的根目录。

vktxenjb

vktxenjb5#

在centOS7上,我更改了文件/etc/httpd/conf/httpd.conf
从AllowOverride None到AllowOverride All

ggazkfy8

ggazkfy86#

我终于在docker-compose文件的初始启动/运行中完成了这个工作,只需在docker文件中使用RUN和CMD命令。我必须在启用mod重写后重新启动Apache。

# syntax=docker/dockerfile:1

FROM php:8.1.1-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli && a2enmod rewrite;
CMD apachectl -k restart; apachectl -D FOREGROUND
ENTRYPOINT [ "a2enmod rewrite" ]

我的.htaccess文件中也有以下内容,该文件位于我的php服务器根目录的根目录中。

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>

相关问题