1

我正在尝试重写 URL 中的破折号,以便它们在内部不存在。例如这个网址

本地主机/我的站点/关于我

"about-me"应该重写为"aboutme"。我需要这个,因为控制器类名取决于这个路由字符串,我显然不能为此使用破折号。

这是我发现的条件和规则,我认为应该适合我的需要:

# Condition is to avoid rewrite on files within specified subdirs
RewriteCond $1 !^(css|img|ckeditor|scripts)
RewriteRule ^([^-]+)-([^-]+)$ $1$2 [L]

然而,它似乎不起作用,因为控制器类Aboutme没有实例化。相反,我收到了404 错误,并且对于名称中没有破折号的类似控制器类,我没有任何问题。

你能帮我解决这个问题吗?

4

4 回答 4

4

为什么不走路线?

$route['about-me'] = 'aboutme/index';
于 2012-12-18T10:42:53.810 回答
1

尝试删除 ^ 和 $

# Condition is to avoid rewrite on files within specified subdirs
RewriteCond $1 !^(css|img|ckeditor|scripts)
RewriteRule ([^-]+)-([^-]+) $1$2 [L]
于 2012-12-18T10:44:26.353 回答
1

您可以扩展路由器类。
/application/core创建一个名为MY_Router.php的文件(MY是默认前缀)并将其复制到其中;

<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {

    function set_class($class) {
        $this->class = str_replace('-', '_', $class);
    }

    function set_method($method) {
        $this->method = str_replace('-', '_', $method);
    }

    function _validate_request($segments) {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).'.php')) {
            return $segments;
        }
        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0])) {       
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            if (count($segments) > 0) {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).'.php')) {
                    show_404($this->fetch_directory().$segments[0]);
                }
            } else {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        // Can't find the requested controller...
        show_404($segments[0]);
    }
}

这将自动为您重写 - 为 _ 。
如果您不想使用下划线,请更改代码以将其替换为空;
的所有str_replace('-', '_',出现str_replace('-', '',

于 2012-12-18T11:52:26.137 回答
1

这是一种使用 mod-rewrite 的方法:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  ^/(.*)([\w]*)-([\w]*)(.*)/?$ [NC]
RewriteRule  .* %1%2%3%4 [L,DPI]

不会重定向,但可以R=301像这样添加 [R=301,DPI,L]。

不一定要about-me。可以是任何位置的任何单词对。IE

localhost/mysite/about-me=.../aboutme

localhost/mysite/folder1/folder2/folder3/my-folder=.../myfolder

localhost/mysite/folder1/folder2/my-folder/folder3= .../myfolder/...

于 2012-12-18T16:23:38.510 回答