PHP简单的动态URL路由
嗨,我正在尝试为我的数据库驱动的网站创建一个非常简单的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试从请求URI中删除查询。
而且您不能将查询参数贴在包含文件的路径中。但是,如果您希望随附的脚本表现得像提交一样,则可以填充GET参数。但是从外观上看,我认为您应该避免这种情况。
从长远来看,将查询字符串附加到路线上,也许这个.htaccess更好。这是最后一个可能与其他人相冲突的规则:
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.
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: