路由 URL 时使用 index.php?q=path/ 而不是 index.php/path/ 的优点/缺点?

发布于 2024-12-08 02:17:31 字数 529 浏览 0 评论 0原文

我正在编写一个简单的方法来将路由映射到文件,并且我遇到了两种方法来实现它。

第一种,我猜大多数框架都会使用,是使用 $_SERVER['REQUEST_URI'] 变量来提取 index.php 之后的所有内容:

RewriteRule ^(.*)$ index.php [QSA,L]

第二种方法在 Drupal 中使用,路由只是作为查询字符串传递。

RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]

现在,“Drupal 方式”对我来说似乎简单多了。使用另一种方法,您必须在 $_SERVER['REQUEST_URI'] 和 $_SERVER['SCRIPT_NAME'] 上使用“explode”,然后使用 array_diff_assoc 之类的内容删除脚本名称和子目录名称(如果有)。这并不是很多工作,但如果使用 Drupal 方式您可以简单地提取 $_GET['q'] 值,为什么没有人这样做呢?如果有的话,有哪些缺点?

谢谢。

I'm writing a simple method to map routes to files and I've come across two ways to do it.

The first, and I guess used by most frameworks, is using the $_SERVER['REQUEST_URI'] variable to extract everything after index.php:

RewriteRule ^(.*)$ index.php [QSA,L]

The second way is used in Drupal, and the route is simply passed as a query string.

RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]

Now, the "Drupal way" seems a lot simpler to me. With the other method you'd have to use "explode" on both $_SERVER['REQUEST_URI'] and $_SERVER['SCRIPT_NAME'] and then use something like array_diff_assoc to remove the script name and subdirectory name, if there is one. It's not THAT much work, but if with the Drupal way you can simply extract the $_GET['q'] value, why nobody does it that way? What are the disadvantages, if any?

Thanks.

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

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

发布评论

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

评论(3

倾听心声的旋律 2024-12-15 02:17:31

使用 q 参数的缺点是,如果没有 URL 重写,URL 将看起来像...

http://domain.com/?q=something

...而不是更干净的(IMO)...

http://domain.com/index.php/something

The disadvantage of using a q param is, without URL rewriting the URL will look like...

http://domain.com/?q=something

...as opposed to the cleaner (IMO)...

http://domain.com/index.php/something
我们只是彼此的过ke 2024-12-15 02:17:31

重写 url 没有任何巨大的优点或缺点。不过,我会指出包括最后一个斜杠在内的所有内容都存储在 _SERVER[PATH_INFO] 中,因此不需要解析请求 URI。

There is no huge advantage or disadvantage one way or the other with the url being rewritten. However, I will point out everything including and after the final slash is stored in _SERVER[PATH_INFO], so parsing the request URI my not be necessary.

请恋爱 2024-12-15 02:17:31

大多数使用较短 URL 技术的原因是为了更干净的技术和由此带来的更好的 SEO。搜索引擎认为这两个 URL“相同”:

http://www.domain.com/?b=something

http://www.domain.com/?b=hello

我没有很好的解释,所以这里是一些链接,其中包含一些非常好的信息:


现在有些人以不同的方式实现较短的网址,但这就是我发现他们的方式对我来说效果最好:

在 .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

在 index.php (或其他一些 php 文件)

if(isset($_GET['route']) && $_GET['route'] != NULL && strlen($_GET['route']) > 0)
{
    $split = explode('/', $_GET['route']);
    for($i=1; $i <= count($split)-1; $i++)
    {
        $_GET[$i] = $split[$i];
    }
}

中这允许您使用 $_GET['1'] (或 $_GET[1])以及所有后续数字。

URL 看起来像这样:

http://www.domain.com/?b=something

变成了

http://www.domain.com/something

http://www.domain.com/?b =某事&a=你好&c=blah

变成了

http://www.domain.com/something/hello/blah

然后可以通过以下方式访问参数:

$_GET[1] = "something";
$_GET[2] = "hello";
$_GET[3] = "blah";

希望有帮助!

The reason that the shorter URL technique is used mostly is for the cleaner technique and the better SEO that comes from it. Search engines consider these two URL's to "be the same":

http://www.domain.com/?b=something

http://www.domain.com/?b=hello

I do not have a good explanation so here are some links with some really good information on it:


Now some people implement the shorter URL's differently, but this is how I have found them to work the best for me:

In .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

In index.php (or some other php file)

if(isset($_GET['route']) && $_GET['route'] != NULL && strlen($_GET['route']) > 0)
{
    $split = explode('/', $_GET['route']);
    for($i=1; $i <= count($split)-1; $i++)
    {
        $_GET[$i] = $split[$i];
    }
}

This then allows you to use $_GET['1'] (or $_GET[1]) and all subsequent numbers as well.

URL's then look like this:

http://www.domain.com/?b=something

becomes

http://www.domain.com/something

http://www.domain.com/?b=something&a=hello&c=blah

becomes

http://www.domain.com/something/hello/blah

And then the parameters can be accessed via:

$_GET[1] = "something";
$_GET[2] = "hello";
$_GET[3] = "blah";

Hope that helps!

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