PHP Url 重定向 index.php

PHP Url redirection on index.php

我正在自学 php 并开始为门户网站编写一些代码。我想用 url 开发门户,它应该看起来像 index.php?page=dashboard。如果我把 dashboard.php 放在浏览器中,它不应该显示。

我有疑问。

1.how 编程使用像 index.php?page=dashboard 这样的页面,而不是像 dashboard.php

这样有单独的 php 文件
  1. 如何使 url 看起来像从 www.example.com/index.php?page=dashboard 到 www.example.com/dashboard

您可以为此使用 apache mod_rewrite,即:

在您网站的根目录下创建一个名为 .htaccess 的文件并放入以下内容:

RewriteEngine On
RewriteRule ^dashboard$ /dashboard.php [L]

这样,当用户导航到 www.yoursite.com/dashboard 时,apache 会将他重定向到 dashboard.php 而不会更改 url.

我认为阅读这篇关于 URI 组件的 wiki article 可能有助于您理解 URL 的部分内容。

简而言之,

  • ?表示 query/search 通常发送到服务器以查询数据库
  • # 表示片段或部分,不发送到服务器,仅由客户端读取。

如果您可以更正问题或提供更好的示例以使其更清楚。

即。 index.php?仪表板 无效。

希望对您有所帮助。

UPDATE Pedro Lobito 在下面给出了一个解决方案,希望这就是您要问的。

您可以在 server/local 上创建一个新文件夹并在其中创建一个新索引。html/index.php,因此如果用户打开 www.example.com/dashboard/ 它在索引中显示 url 但用户在索引页面上。