2013-10-27 13 views
7

Tôi đang cố viết một vài quy tắc mod_rewrite. Mọi thứ đang hoạt động tốt tại thời điểm này. Tại thời điểm này URL generic của tôi trông như thế này:Mod_rewrite kiểm tra xem tập tin php có tồn tại không?

http://website.com/about-us 
http://website.com/privacy 

Tại thời điểm này, những 2 liên kết được mod_rewritten để content.php, vì about-usprivacy không tồn tại file như .php, nhưng đúng hơn, nội dung của họ được lưu trữ trong một cơ sở dữ liệu mà content.php truy xuất.

Tôi có một url:

http://website.com/contact-us 

nào không tồn tại như là một file .php, bởi vì nó chứa dữ liệu tùy chỉnh. Làm cách nào để kiểm tra xem liệu có tồn tại contact-us.php hay không. Nếu có, chuyển hướng website.com/contact-us-website.com/contact-us.php, nếu không, chuyển hướng đến website.com/content.php

Dưới đây là tập tin mod_rewrite của tôi như nó đứng:

RewriteEngine On 

# I want the following condition and rule to look to see if the .php file exists, 
# and if it does, redirect to the physical page, otherwise, redirect to content.php 
RewriteCond %{DOCUMENT_ROOT}/([a-zA-Z0-9\-_]+).php -f 
RewriteRule ^([a-zA-Z0-9\-_]+)\.php$ [L] 


RewriteRule ^([a-zA-Z0-9\-_]+)$ /content.php [L] 
RewriteRule ^(product1|product2|product3)/(designer1|designer2|designer3)$ /search.php?productType=$1&designer=$2 [L] 
RewriteRule ^sale/(product1|product2|product3)$ /search.php?sale=1&productType=$1 [L] 

Nếu bạn cần thêm thông tin, xin vui lòng cho tôi biết! Tôi đánh giá cao trả lời :)

Trả lời

9

Thay đổi mã .htaccess của bạn để

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d # not an existing dir 
RewriteCond %{REQUEST_FILENAME} !-f # not an existing file 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f # and page.php exists 

# redirect to the physical page 
RewriteRule ^(.*)$ $1.php [L] 

# otherwise, redirect to content.php 
RewriteRule^/content.php [L] 

RewriteRule ^sale/(product[123])$ /search.php?sale=1&productType=$1 [QSA,NC,L] 
RewriteRule ^(product[123])/(designer[123])$ /search.php?productType=$1&designer=$2 [QSA,NC,L] 

Tôi cũng đã đơn giản hóa mô hình phù hợp cho RewriteRule bạn s khác. Lưu ý việc sử dụng [QSA] để tự động chuyển tiếp bất kỳ thông số truy vấn bổ sung nào và [NC] để làm cho trường hợp không phân biệt chữ hoa chữ thường.

+0

Cảm ơn bạn đã trả lời, thật không may, nó đến với một lỗi 500 máy chủ nội bộ :( –

+0

Tháo '/ 'giữa'% {DOCUMENT_ROOT}% {REQUEST_URI} ' –

+0

Ngoài ra, hãy xem xét kết hợp mẫu đơn giản hơn cho 'RewriteRule's. –

1

Tôi sẽ sử dụng này:

# 
# redirect first the evidences: 
# 
RewriteRule ^(product1|product2|product3)/(designer1|designer2|designer3)$ /search.php? productType=$1&designer=$2 [L] 
RewriteRule ^sale/(product1|product2|product3)$ /search.php?sale=1&productType=$1 [L] 

# 
# redirect then the generality: 
# 
RewriteCond %{DOCUMENT_ROOT}%%{REQUEST_URI}.php -f 
RewriteRule^%{DOCUMENT_ROOT}%%{REQUEST_URI}.php [L] 

# 
# all other requests are rewritten to /content.php: 
# 
RewriteRule^/content.php [L] 
Các vấn đề liên quan