0

我有 joomla 2.5 站点,并且我有 http://www.something.com/places?x=target

我想要这样的网址:http: //www.something.com/places/target

我怎样才能做到这一点?

编辑:

在 .htaccess 上的以下作品: RewriteRule ^places/(.*)$ http://www.something.com/places?x= $1 [L,P,nc]

但是它不适用于空格('/places/tar get' 只会转到 '/places?x=tar')。我该如何解决?

编辑2:

RewriteRule ^places/([^\ ] )\ (. )$ http://www.something.com/places?x= $1\%20$2 [L,P,nc]

RewriteRule ^places/(.*)$ http://www.something.com/places?x= $1 [L,P,nc]

诀窍。谢谢你们!

4

1 回答 1

2

如果位置页面属于自定义组件(不是 Joomla! 内置组件),您将需要在组件目录中编写或调整router.php文件。

它需要包含以下内容:

function yourcomponentnameBuildRoute(&$query) {

    $segments       = array();

    if (isset($query["x"])) {  
        $segments[] = $query["x"];  
        unset($query["x"]); 
    }

    return $segments;

}

function yourcomponentnameParseRoute($segments) {

    $vars   = array();
    $count  = count($segments);

    switch($segments[0]) {

        case "target":
            $vars["x"]              = "target";
            break;

    }

    return $vars;

}

更新您的具体情况:

不幸的是,没有核心黑客就没有办法做到这一点。

所以备份你的 *components/com_content/router.php* 文件,然后编辑它如下:

替换以下代码(大约第 132 行):

if ($view == 'article') {
    if ($advanced) {
        list($tmp, $id) = explode(':', $query['id'], 2);
    }
    else {
        $id = $query['id'];
    }
    $segments[] = $id;
}
unset($query['id']);
unset($query['catid']);

有了这个:

if ($view == 'article') {
    if ($advanced) {
        list($tmp, $id) = explode(':', $query['id'], 2);
    }
    else {
        $id = $query['id'];
    }
    if(isset($query['x']) && $query['x']) {
        $segments[] = $query['x'];
    }
    $segments[] = $id;
}

unset($query['x']);
unset($query['id']);
unset($query['catid']);

和这段代码(大约第 212 行):

if (!isset($item)) {
    $vars['view']   = $segments[0];
    $vars['id'] = $segments[$count - 1];

    return $vars;
}

// if there is only one segment, then it points to either an article or a category
// we test it first to see if it is a category.  If the id and alias match a category
// then we assume it is a category.  If they don't we assume it is an article
if ($count == 1) {
    // we check to see if an alias is given.  If not, we assume it is an article
    if (strpos($segments[0], ':') === false) {
        $vars['view'] = 'article';
        $vars['id'] = (int)$segments[0];
        return $vars;
    }

有了这个:

if (!isset($item)) {
    $vars['view']   = $segments[0];
    $vars['id'] = $segments[$count - 1];
    $vars['x']      = $count >= 2 ? $segments[$count - 2] : NULL;
    return $vars;
}

// if there is only one segment, then it points to either an article or a category
// we test it first to see if it is a category.  If the id and alias match a category
// then we assume it is a category.  If they don't we assume it is an article
if ($count == 1 || ($count == 2 && (int) $segments[0] === 0)) {
    // we check to see if an alias is given.  If not, we assume it is an article
    if (strpos($segments[0], ':') === false) {
        $vars['view'] = 'article';
        $vars['x'] = $count == 2 ? $segments[$count - 2] : NULL;
        $vars['id'] = (int)$segments[$count - 1];
        return $vars;
    }

然后在您文章的 PHP 代码中,您将使用:

$target = JRequest::getVar("x");

我没有测试过,所以我不确定它是否有效。让我知道。

于 2013-09-24T07:37:34.057 回答