PHP / Apache:GET请求不存在?

时间:2010-10-17 17:45:40

标签: php apache mod-rewrite httpwebrequest get

基本上我的网站根目录中的.htaccess文件中有以下内容:

Options -Indexes

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
</IfModule>

在我的PHP脚本中,当我使用$ _GET ['route']时,我收到以下错误:

Notice: Undefined index: route

我不明白为什么这不起作用? 我以前在以前的网站上使用过这段代码来获取友好的URL,而且PHP脚本的GET请求值很好,但它现在似乎正在播放:/

当我手动执行http://localhost/index.php?route=hmm时,错误就会消失,我可以获得$ _GET ['route']的值

我做错了什么? 询问您是否需要任何其他信息! 谢谢你的阅读。

1 个答案:

答案 0 :(得分:0)

我用它来进行URI重写(路由):

Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

并解析:

class Dispatcher {
    [snip]
    private static function parse_uri() {
        if (self::$uri_parsed === false) {
            // Clean URI
            $uri = preg_replace('~|/+$|/(?=/)~', '', $_SERVER['REQUEST_URI']);

            // Strip get variables from request
            $get_position = strpos($uri, '?');
            if ($get_position !== false) {
                $striped_get = substr($uri, 0, $get_position);
            } else {
                $striped_get = $uri;
            }

            // Get the file and directory of the request
            if (strstr($striped_get, '.') != false) {
                // Strip the file from the URI
                $slash_position = strrpos($striped_get, '/');
                if ($slash_position !== false) {
                    $striped_file = substr($striped_get, 0, $slash_position + 1);
                } else {
                    $striped_file = $striped_get;
                }
                self::$command = $striped_file;
                self::$file    = substr($striped_get, strlen(self::$command));
            } else {
                self::$command = $striped_get;
                self::$file    = '';
            }
            // Trim slashes and replace dashes with underscores
            self::$command = str_replace('-', '_', trim(self::$command, '/'));

            if (DEBUG) {
                // Log the results
                Logger::log('Command: '.self::$command, self::LOG_TYPE);
                Logger::log('File: '.(self::$file ? self::$file : 'N/A'), self::LOG_TYPE);
            }

            self::$uri_parsed = true;
        }
    }
    [snip]
}