mod_rewrite错误

时间:2011-07-15 15:10:57

标签: .htaccess mod-rewrite

我在新网站上使用mod_rewrite。

.htaccess文件

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rwr=$1

index.php文件

if (isset($_REQUEST['rwr'])) {
    if (substr($_REQUEST['rwr'], -1) == "/") {
        $modrewrite = substr($_REQUEST['rwr'], 0, -1);
    } else {
        $modrewrite = $_REQUEST['rwr'];
    }
    $modrewrite = explode("/", $modrewrite);
}
if (isset($modrewrite) && $modrewrite[0] != "") {
    $category = $modrewrite[0];
} else {
    $category = null;
}

if (isset($modrewrite[1])) {
    $service = $modrewrite[1];
} else {
    $service = null;
}

if (isset($modrewrite[2])) {
    $identification = $modrewrite[2];
} else {
    $identification = null;
}

使用此链接www.domain.com/service/webdesign/works转到 -

$category = "service";
$service = "webdesign";
$identification = "works";

这有效但我想优化它:

.htaccess文件

RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)$ index.php?category=$1&service=$2&identification=$3

index.php文件

$category = $_GET['category'];
$service = $_GET['service'];
$identification = $_GET['identification'];

但它不起作用。为什么?任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您的规则应该适用于相关网址。它可能是您的Apache配置和/或类似命名的文件导致它。让我们以您的示例网址/service/webdesign/works为例。您可能拥有名为service.php的文件,然后Apache可能会因某种原因忽略该重写规则。

您可以使用此方法:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

# the rewrite rule
RewriteRule ^([a-z0-9\-_]*)(/([a-z0-9\-_]+))?(/([a-z0-9\-_]+))?$ index.php?category=$1&service=$3&identification=$5 [NC,QSA,L] 

这是经过测试和运作的解决方案。