Apache URL重写无法正常工作

时间:2013-11-02 13:27:31

标签: apache .htaccess url-rewriting

我在.htaccess中遇到网址重写问题。以下是文件摘录:

AddDefaultCharset utf-8

RewriteEngine On
RewriteRule ^(content\.php\?page=)(.+)$ $1.php [NC]

我需要将"content.php?page=quality-policy"之类的网址重写为"quality-policy.php"

但我明白这一点:

Not Found
The requested URL content.php was not found on this server.

1 个答案:

答案 0 :(得分:1)

你应该用这个:

RewriteEngine On
RewriteCond %{QUERY_STRING} page=([^&]*) [NC]
RewriteRule ^content\.php$ %1.php [L]

说明:   如果%{QUERY_STRING}包含page = something,那么

将URL content.php重写为something.php

%1是RewriteCond中的正则表达式反向引用,不要与$ 1混淆,后者是匹配的RewriteRule正则表达式中的第一个反向引用。标志[L]强制规定这是要执行的最后一条规则,忽略后面的所有规则。