【问题标题】:PHP parameter / Nested folder / CMSPHP 参数/嵌套文件夹/CMS
【发布时间】:2014-12-01 19:46:22
【问题描述】:

假设我有一个网站birdovertheworld.com/,我想以我所知道的鸟类的形式添加一些内容。我(据我所知)可以做任何一个

a)birdovertheworld.com/bird/hummingbird/

b)birdovertheworld.com/bird/show.php?bird=hummingbird

使用 a) 似乎很愚蠢,因为它需要我手动为每只鸟创建一个文件夹 + 一个索引文件。

另一方面,使用 b) 会(再次,就我而言)会损害 SEO 和可读性。

我熟悉的 CMS 不适用于我的目的(WordPress、Joomla)+ 在这种特殊情况下我宁愿不使用它。

我的问题是: - 如何在手动创建文件夹和索引文件并且与示例b一起使用的情况下实现用户和搜索引擎易于阅读的URL?

谢谢

【问题讨论】:

  • 你在用apache吗?
  • 是的,我现在是 :-o?

标签: php seo content-management-system nested


【解决方案1】:

a) 实际上是一个有效的选项。我们通过强制 Apache 将某些脚本读取为 PHP 来做到这一点,即使它们没有文件扩展名。

所以,“bird”是一个 PHP 脚本,它读取调用的 URL 并包含/生成根据 url 字符串的内容而变化的内容:

$called = $_SERVER['REQUEST_URI'];
$parts = explode("/",$called);
$birdname = $parts[1];
switch($birdname) { // or whatever logic.

Apache 配置使用 ForceType application/x-httpd-php5。

【讨论】:

    【解决方案2】:

    例如,如果您查看 WordPress .htaccess 文件,您会看到如下内容:

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

    这告诉 Apache 将浏览器正在访问的任何不存在的文件或文件夹重定向到 index.php

    然后,在 PHP 端,您会在 $_SERVER['REQUEST_URI'] 变量中获取原始请求的 url。

    最后,您可以使用explode(...) 访问请求的路径:

    $path = explode('/', $_SERVER['REQUEST_URI']);
    switch($path[1]){
        case 'bird':
            //do your things
            break;
        default:
            //404
    }
    

    请记住,您必须正确处理 404 errors,因为 Apache 不会知道那只特定的鸟是否存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多