PHP/Apache:GET 请求不存在?

发布于 2024-09-28 01:11:36 字数 666 浏览 4 评论 0原文

基本上,我的网站根目录下的 .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[' 的值路线']

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

Basically I have the following in my .htaccess file in the root of my site:

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>

In my PHP script when I use $_GET['route'] I get the following error:

Notice: Undefined index: route

I don't understand why this isn't working?
I've used this code in the past on a previous website for friendly URLs and the PHP script got the GET request value fine, but it seems to be playing up now :/

When I do it manually like http://localhost/index.php?route=hmm the error goes away and I can get the value of $_GET['route']

What am I doing wrong?
Ask if you need any additional information!
Thanks for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜 2024-10-05 01:11:36

我用它来重写 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]
}

I use this for URI rewriting (routing):

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

And parse it with:

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]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文