apache mod_rewrite не работает или не включен - PullRequest
43 голосов
/ 19 октября 2011

Я установил rewrite_module и изменил php.ini на Apache.

Я создаю 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]

/ вар / 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>

Ответы [ 5 ]

85 голосов
/ 04 ноября 2011

Попробуйте установить: «AllowOverride All».

11 голосов
/ 19 октября 2011

Пожалуйста, попробуйте

sudo a2enmod rewrite

или используйте правильную команду перезапуска apache

sudo /etc/init.d/apache2 restart 
4 голосов
/ 19 октября 2011

Работает.

мое решение:

1.создать test.conf в /etc/httpd/conf.d/test.conf

2. написал какое-то правило, например:

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

3. перезагрузите сервер Apache.

4. Попробуй снова сам.

1 голос
/ 24 июля 2017

Чтобы заставить mod_rewrite работать на меня в Apache 2.4, мне пришлось добавить строку «Требовать все предоставленные» ниже.

<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 и посмотрите, работает ли она. Приведенный выше пример можно использовать для проверки того, что сайт не обслуживается из кэша, путем включения в путь подпапки, хотя файлы находятся в корне сервера.

0 голосов
/ 26 августа 2017

В centOS7 я изменил файл /etc/httpd/conf/httpd.conf

с AllowOverride None на AllowOverride All

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...