【问题标题】:How to use $_GET parameter to make seo friendly urls?如何使用 $_GET 参数使 seo 友好的 url?
【发布时间】:2013-06-05 14:24:50
【问题描述】:

我正在使用我自己制作的 PHP CMS。它获取 $_GET 参数url 并将其转换为www.website.com/{url}。此 CMS 使用 $_GET 参数来获取文件。因此,如果 urlindex,则 CMS 将搜索文件 index 并返回文件的内容。但现在,我正在扩展 CMS 以添加一个像 profile/{username} 这样的参数。我该如何扩展它?我希望我的 URL 显示 www.website.com/profile/{username}。我该怎么做呢?这是我当前的 htaccess:

RewriteEngine On  
RewriteRule ^(|/)$ index.php?url=$1  
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1a

这里是 TPL 系统。

public function addTpl() {     
    global $zip;

    if(!isset($_GET['url']) || empty($_GET['url'])) {
        $_GET['url'] = 'index';
    }

    if(file_exists('_zip/_templates/_front/'. $zip['Template']['Front'] . '/')) {
        if(file_exists('_zip/_templates/_front/'. $zip['Template']['Front'] . '/' . secure($_GET['url']) . '.php')) {
            ob_start();
            include('_zip/_templates/_front/'. $zip['Template']['Front'] . '/' . secure($_GET['url']) . '.php');
            $this->tpl .= ob_get_contents();
            ob_end_clean();
        } else {
            die(zipError('File Not Found', 'The file <b>' . secure($_GET['url']) . '</b> could not be found. Please re-check the URL; If you were directed here using a link, please report that link.'));
        }
    } else {
        die(zipError('Template Not Found', 'The template <b>' . $zip['Template']['Front'] . '</b> could not be found. Please check your configuration file for a mistake.'));
    }
}

我还需要提供其他信息吗?我需要尽快完成这项工作,因此我们将不胜感激。

【问题讨论】:

  • 我更新了主帖。我为 TPL 系统添加了 PHP。
  • “seo 友好的 url”是 SEO 行业犯下的神话。
  • @Dagon 也许是真的,但它们确实看起来更好,看到它们只会让你想拥有它们。
  • 我在乎外观或网址就像我在乎我家钥匙的外观一样
  • 你没有灵魂先生

标签: php .htaccess get seo


【解决方案1】:

在包含文件之前,您可以将第二个参数 ( {username} ) 作为局部变量,然后您可以在其中将其作为普通变量进行访问。如果您与团队合作,这不是一件好事。

编辑

你可以做的是以下

RewriteEngine On  
RewriteRule ^ index.php

然后在 index.php 你可以有这个

$url = str_replace('index.php','', $_SERVER['PHP_SELF']);
$url = str_replace($url,'',$_SERVER['REQUEST_URI']);
$url = explode('/',$url);
$page = array_shift($url);
foreach ($url as $val){
    $args[] = urldecode($val);
}

现在如果你打开链接www.website.com/profile/someuser/about你会得到

$page = 'profile'
$args[0] = 'someuser'
$args[1] = 'about'

等等,你可以有尽可能多的参数。

【讨论】:

  • 什么意思?你能详细说明一下吗?这是我正在制作的社交网站。
  • @IntactDev 编辑了我的答案
  • 不要认为这会起作用,因为您没有将 RegEx 规则中的任何变量传递给 Apache 重写中的 index.php...
  • @Phillip 它可以工作,这也是 laravel 的工作方式。
【解决方案2】:

php_nub_qq 的回答更简洁:从长远来看,您最好使用$_SERVER['REQUEST_URI'],而不是每次您想在 CMS 中添加新的细分时添加更多的重写规则。但无论如何我已经在下面回答了,假设您不想过多地更改现有代码,因为您处于困境之中。

此外,您还要尽量避免在 addTpl() 中设置任何变量。这是有充分理由的,正如 php_nub_qq 也已经指出的那样,这是为了避免在子模板中设置任何违反封装的变量。但这是一个繁琐的限制,因为您必须向该方法添加更多逻辑,因此您可能应该将模板包含因素分解为私有方法。然后你可以在 addTpl() 中做任何你想做的复杂逻辑。为了避免在模板文件中设置除$this 之外的任何变量,您可以在模板对象本身上设置模板文件的路径,并在将其传递给模板包含方法时使用该路径。

因此,您将添加另一个重写规则:

RewriteRule ^profile/([a-zA-Z0-9_-]+)(|/)$ index.php?username=$1

编辑:我删除了破坏这个的正则表达式开头的额外斜杠。

这将在$_GET 中设置用户名键,您可以使用它来切换要加载的模板。然后在 addTpl() 所在的任何类中执行以下操作(这也假设您更改配置以添加类似于$zip['Template']['Front']$zip['Template']['Profile'] 以设置配置文件模板的目录:

class Template {
    public $tpl = '';
    private $_template_file;

    public function addTpl() {     
        global $zip;

        if(!isset($_GET['url']) || empty($_GET['url'])) {
            $_GET['url'] = 'index';
        }

        $top_dir = '_zip/_templates/';
        $type_dir = '_front/';
        $configured = $zip['Template']['Front'];
        $file = $_GET['url'];

        // Check username val and use custom template:
        if (!empty($_GET['username'])) {
            $type_dir = '_profile/';
            $configured = $zip['Template']['Profile']; // assuming you're configuring profile templates
            $file = $_GET['username'];
        }

        $full_dir = $top_dir . $type_dir . $configured . '/';
        if(file_exists($full_dir)) {
            $full_file = $full_dir . secure($file) . '.php';
            if(file_exists($full_file)) {
                $this->_template_file = $full_file;
                $this->tpl .= $this->loadTemplate();
            } else {
                die(zipError('File Not Found', 'The file <b>' . secure($file) . '</b> could not be found. Please re-check the URL; If you were directed here using a link, please report that link.'));
            }
        } else {
            die(zipError('Template Not Found', 'The template <b>' . $configured . '</b> could not be found. Please check your configuration file for a mistake.'));
        }
    }

    private function loadTemplate() {
        ob_start();
        include($this->_template_file);
        $result = ob_get_contents();
        ob_end_clean();
        return $result;
    }
}

【讨论】:

    猜你喜欢
    • 2013-01-20
    • 2016-11-16
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2018-09-10
    • 2011-10-28
    相关资源
    最近更新 更多