2012-12-18 59 views
1

我想重写URL中的破折号,以便它们不存在于内部。例如这个URL重写规则:试图摆脱破折号

本地主机/ mysite的/ aboutme

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

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

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

但是似乎它不工作,因为控制器类Aboutme没有实例化。我得到一个404错误,而我没有任何问题与类似的控制器类没有破折号在他们的名字。

你能帮我一下吗?

+0

好,进入filepath是**本地主机/ mysite的/ index.php的**,但是我已经在另一对条件规则中处理这个条件:** RewriteCond $ 1!^(index.php | css | img | scripts | ckeditor | robots.txt | sitemap.xml | sitemap.xml.gz) RewriteRule ^(。*)$指数。php/$ 1 [L] ** –

+0

如果我正确注册,'-'是一个正则表达式条件,你用'\ -' –

+0

尝试过我也试着用'^([^ \ - ] +) \ - ([^ \ - ] +)$'和'^([^ \ - ] +) - ([^ \ - ] +)$'问题依然存在。 –

回答

4

为什么不走路线?

$route['about-me'] = 'aboutme/index'; 
+0

是的。这是我考虑的一个选项,但我试图用Apache来完成。我应该可以做到。如果我没有得到它,我会选择Codeigniter路线。 –

+0

我一定会走路线。特别是当事情变得动态的时候,比如'$ route ['product /(:num)/([az] +)'] ='product/view/$ 1/$ 2';' – Mudshark

+0

我会接受你的建议并且使用路线。无论如何,如果我们能够找出为什么我的重写规则不起作用,那将会更好。 –

1

尝试删除^和$

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

完成,但我得到同样的问题。 –

+0

RewriteRule([^ \ - ] +)( - )[^ [ - ] +)$ 1 $ 2 [L] –

1

可以扩展路由器类。
/应用/核心创建一个名为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('-', '',

+0

在路由中使用正则表达式比重写路由器类更好吗? Mudshark建议我已经使用路线。不过,如果我也可以使用mod_rewrite来弄清楚如何在Apache服务器中执行它,那将是非常棒的。 –

+0

我一直使用扩展路由器类。这种方式不会使代码部署在哪个平台上,它是自包含的。我经常需要做IIS上的网站,这样可以节省web.config的时间。 – Rooneyl

1

这里所有出现的是一种与国防部重写做到这一点:

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

不会重定向,但能做到这一点添加R=301这样[R = 301,DPI, L]。

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

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

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

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

+0

在我的情况下,我只希望它只能在路线段中工作,而不是在真实的子目录中。想象一下,这就是我正在使用的:'localhost/my-site/about-me'。 **我的网站**是一个真正的目录到我的本地网站。我只想删除路由段中的破折号,例如** about-me **,因为它被路由到一个php控制器类,并且它不能有任何破折号。你应该建议什么? –

+0

我认为它的工作原理是一样的。我的例子假定文件夹不存在,除了最后一个。我只是试图给算法提供更多的功能。试一试。 –