【问题标题】:How to get entry from MYSQL depending on the entered url?如何根据输入的 url 从 MYSQL 获取条目?
【发布时间】:2021-11-14 18:51:15
【问题描述】:

我想要实现的是维基百科正在使用的东西: 您在浏览器搜索栏中输入“wikipedia.org/wiki/Stack_Overflow”,维基百科会向您显示“Stack_Overflow”的文章。 我知道我可以使用 php 的 GET 和 www.website.com/article.html?article_name 做一些事情 但我想知道维基百科的解决方案是如何工作的。

【问题讨论】:

  • 可能正是您想要的方式。怎么办
  • 你暗示你正在使用 PHP;您正在使用一些 php 框架吗?

标签: javascript php mysql


【解决方案1】:

您需要将 ALL 重写为 index.phprouter.php

因此,在您的 .htaccess 中,您可以尝试以下操作:

# Rewrite ALL to index.php
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

然后您可以在index.php 文件中处理请求。

你可以试试这样的:

$REQUEST_URI = $_SERVER['REQUEST_URI'] ?? ""; // get the REQUEST_URI
$reqguestedURL = trim($REQUEST_URI, '/'); // remove the leading and trailing '/'
$reqguestedURL = filter_var($reqguestedURL, FILTER_SANITIZE_URL); // sanitize the URL
$URL_array = explode('?', $reqguestedURL); // explode the URL
$destination = $URL_array[0]; // remove the query string
$queryString = $URL_array[1] ?? ""; // get the query string
$destinationParts = explode('/', $destination); // finally get the destination parts.

// so if the URL is: example.com/foo/bar?name=value
var_dump($REQUEST_URI); //OUTPUT: /foo/bar?name=value
var_dump($reqguestedURL); //OUTPUT: foo/bar?name=value
var_dump($URL_array); //OUTPUT: Array ( [0] => foo/bar [1] => name=value )
var_dump($destination); //OUTPUT: foo/bar
var_dump($queryString); //OUTPUT: name=value
var_dump($destinationParts); //OUTPUT: Array ( [0] => foo [1] => bar )

现在您可以从数据库中获取值了。

【讨论】:

    【解决方案2】:

    您可以像 WordPress 一样执行此操作。 这意味着您将特定目录中的所有http请求重新路由到index.php(无需重定向),然后您可以通过index.php文件处理您想要的内容。

    在此处查看 WordPress 默认的 .htaccess 文件内容: https://wordpress.stackexchange.com/a/45899

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 2017-06-30
      • 2021-05-16
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 2022-07-26
      相关资源
      最近更新 更多