2011-10-19 30 views
37

Tôi đã cài đặt rewrite_module và sửa đổi php.ini trên Apache.apache mod_rewrite không hoạt động hoặc không được bật

Tôi tạo các tệp rewrite.php.htaccess nhưng không hoạt động.

thư mục hệ thống tập tin của tôi như:

/var/www/html 
/var/www/html/test 
/var/www/html/test/.htaccess 
/var/www/html/test/rewrite.php 

nội dung /var/www/html/.htaccess là:

$ 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 được cài đặt.

$ ls -l mod_rewrite.so 
-rwxr-xr-x 1 root root 59256 Sep 20 23:34 mod_rewrite.so 

rewrite_module được tải.

$ cat /etc/httpd/conf/httpd.conf | grep mod_rewrite.so 
LoadModule rewrite_module modules/mod_rewrite.so 

AllowOverride thiết

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

    AllowOverride None 

    Order allow,deny 
    Allow from all 
</Directory> 

Trả lời

75

Tôi đã viết về điều này trong một bài báo: http://www.jarrodoberto.com/articles/2011/11/enabling-mod-rewrite-on-ubuntu

Cố gắng thiết lập: "AllowOverride All".

+1

Câu trả lời này có hiệu quả đối với tôi, nếu tôi có thể đưa ra một đề xuất thân thiện, tôi nghĩ sẽ tốt nhất nếu bạn mang thêm một số câu trả lời từ liên kết vào câu trả lời của bạn ở đây. – amgraham

5

Vui lòng thử

sudo a2enmod rewrite 

hoặc sử dụng đúng lệnh apache restart

sudo /etc/init.d/apache2 restart 
+0

nhưng tôi không có lệnh 'a2enmod'. bởi vì, apahce của tôi là [yum install apache] – easter1021

+0

Bạn đang sử dụng hệ điều hành nào? (CentOS, Ubuntu, v.v.) – ASHOK

+1

Hệ điều hành của tôi là "AWS EC2 t1.micro". xin lỗi, bận sau. – easter1021

4

Đó là làm việc.

giải pháp của tôi là:

1.create một test.conf vào /etc/httpd/conf.d/test.conf

2.wrote một số quy tắc, như:

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

3. khởi động lại máy chủ Apache của bạn.

4.thử lại chính mình.

+3

Tốt là bạn đã tìm được giải pháp, nhưng không hoàn thành câu trả lời của mình bằng một câu hỏi khác. – slugster

+0

Lưu ý rằng nếu bạn đặt các chỉ thị này vào httpd.conf thay vì .htaccess, thẻ Thư mục là bắt buộc. – Aaron

0

Để nhận mod_rewrite hoạt động cho tôi trong Apache 2.4, tôi phải thêm dòng "Yêu cầu tất cả được cấp" bên dưới.

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

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

được cho là một yêu cầu tương tự tồn tại cho Apache 2.2 là tốt, nếu bạn đang sử dụng rằng:

<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> 

Lưu ý rằng một chỉ thị ErrorDocument 404 đôi khi có thể ghi đè lên những điều này là tốt, vì vậy nếu nó không làm việc hãy thử nhận xét chỉ thị ErrorDocument của bạn và xem liệu nó có hoạt động hay không. Ví dụ trên có thể được sử dụng để đảm bảo một trang web không được phục vụ từ bộ nhớ cache bằng cách bao gồm một thư mục con trong đường dẫn, mặc dù các tệp nằm ở gốc của máy chủ.

0

On centOS7 Tôi đã thay đổi tập tin /etc/httpd/conf/httpd.conf

từ AllowOverride None để AllowOverride All

Các vấn đề liên quan