2013-07-01 33 views
0

假设如下:删除模式和301重定向到新的URL

mywebsite.com/searches/index/page:x where x is an integer. 

我想删除索引/并重定向到

mywebsite.com/searches/page:x 

我的web应用程序是基于CakePHP的,和我将以下行添加到了webroot目录中的.htaccess文件,但它没有得到任何结果。

RewriteRule ^(.*)/searches/index/(.*)$ searches/$1 [R=301,L] 

我需要知道正确的.htaccess代码,我应该使用。

但是,我问这个问题,因为谷歌网站管理员工具告诉我,有很多软401错误。在我的应用程序的路线我省略了调用由以下行的索引页在routes.php文件:

Router::connect('/searches/index/*', array('controller' => 'error','action'=>'e404')); 
Router::connect('/searches', array('controller' => 'searches', 'action' => 'index','page' => 1)); 
Router::connect('/searches/*', array('controller' => 'searches', 'action' => 'index')); 

所以,如果有另一种解决方案,以克服CakePHP的谷歌问题,我可以理解的知道这一点。

我的蛋糕版本是1.2.11。

回答

1

规则开始处的组将导致规则匹配URI,如mywebsite.com/searches/index/page:x以及URI,如mywebsite.com/foobar/searches/index/page:x。它看起来并不像你想要的那样。

此外,您正在利用backreference替换您的替换模式中的第一个组,导致新的URL永远不会包含page:x部件。相反,它将针对类似mywebsite.com/searches/index/page:x的URI重定向至mywebsite.com/searches,针对类似mywebsite.com/foobar/searches/index/page:x的URI重定向至mywebsite.com/searches/foobar

我想你的规则应该更是这样的:

RewriteRule ^/?searches/index/(.*)$ /searches/$1 [R=301,L] 

注意,除了删除了第一集团,我也是在模式可选取得了领先的斜线,因为它是forbidden with Apache 2.x, while it's necessary with Apache 1.x,我已经在替换部分添加了一个前导斜杠,以确保它将重定向到根目录。

+0

这是否被拒绝投票?很高兴知道答案出了什么问题,以便我可以纠正可能出现的错误,含糊之处等。 – ndm

+0

+1投票它回答所需的问题! – Termis