PHP简单的动态URL路由

发布于 2025-02-05 19:48:31 字数 662 浏览 1 评论 0原文

嗨,我正在尝试为我的数据库驱动的网站创建一个非常简单的Rout。如果有人可以提供帮助,那就太好了。

我的动态链接如下: /views/page.php?page_slug=link-一个

,您可以在下面看到,我希望我的动态链接会,但事实并非如此。我遇到了一个错误,说文件/目录不存在。我想问题是如何在其中(变量)中获得动态URL,以便重新分组?我可能会完全围绕这个问题。

这是我的htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php 

index.php文件。

 <?php

$request = $_SERVER['REQUEST_URI'];


switch ($request) {

    case '':
    case '/':
        require __DIR__ . '/views/index.php';
        break;

    case '/link-one':
        require __DIR__ . '/views/page.php?page_slug=link-one';
        break;
}

任何有助于实现的帮助。

Hi I'm trying to create a really simple rout for my database driven website. Wold be great if someone could help.

My dynamic link is the following:
/views/page.php?page_slug=link-one

As you can see below I was hoping my dynamic link would would but it doesn't. I get an error saying that the file/directory doesn't exist. I guess the question is how would I get a dynamic URL in there (variable) so that it re-routes? I might be going completely the wrong way around this.

Here is my htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php 

index.php file below.

 <?php

$request = $_SERVER['REQUEST_URI'];


switch ($request) {

    case '':
    case '/':
        require __DIR__ . '/views/index.php';
        break;

    case '/link-one':
        require __DIR__ . '/views/page.php?page_slug=link-one';
        break;
}

Any help much appriciated.

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

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

发布评论

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

评论(1

云裳 2025-02-12 19:48:31

尝试从请求URI中删除查询。

而且您不能将查询参数贴在包含文件的路径中。但是,如果您希望随附的脚本表现得像提交一样,则可以填充GET参数。但是从外观上看,我认为您应该避免这种情况。

$request = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

switch ($request) {

    case '/':
        require __DIR__ . '/views/index.php';
        break;

    case '/link-one':
         $_GET['page_slug'] = 'link-one';
        require __DIR__ . '/views/page.php';
        break;

}

从长远来看,将查询字符串附加到路线上,也许这个.htaccess更好。这是最后一个可能与其他人相冲突的规则:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Try removing the query from the request uri.

And you can't stick query parameters in the path to an included file. But you can populate the get parameter if you want the included script to behave like it was submitted. But from the looks of it I think you should avoid that.

$request = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

switch ($request) {

    case '/':
        require __DIR__ . '/views/index.php';
        break;

    case '/link-one':
         $_GET['page_slug'] = 'link-one';
        require __DIR__ . '/views/page.php';
        break;

}

And maybe this .htaccess is better in the long run where query string is appended to the route. And this is the last rule not conflicting others that may follow:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文