【问题标题】:Remove pattern from URL and 301 redirect to the new URL从 URL 中删除模式并 301 重定向到新 URL
【发布时间】:2013-06-28 02:03:12
【问题描述】:

假设如下:

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

我想删除 index/ 并重定向到

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 的 Google 问题,我将不胜感激。

我的蛋糕版本是 1.2.11。

【问题讨论】:

    标签: .htaccess cakephp redirect routes rewrite


    【解决方案1】:

    规则开头的组将导致规则匹配像 mywebsite.com/searches/index/page:x 这样的 URI 以及像 mywebsite.com/foobar/searches/index/page:x 这样的 URI。看起来这不是你想要的。

    另外,您将 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,并且我在替换部分添加了前导斜杠以确保它会重定向到根目录。

    【讨论】:

    • 这是否有理由被否决?很高兴知道答案有什么问题,这样我就可以纠正可能的错误、歧义等。
    猜你喜欢
    • 1970-01-01
    • 2011-09-02
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多