【问题标题】:How can I do this with .htaccess如何使用 .htaccess 做到这一点
【发布时间】:2010-07-16 15:38:17
【问题描述】:

我正在编写一个 PHP (Kohana) 应用程序,我看到控制器索引页面可以在有 & 没有 /index 的情况下看到。

http://plugb.com/ & http://plugb.com/index

为了避免重复内容,我这样做了:

$clean_url = str_replace('index','',$this->request->uri);

if($clean_url !== $this->request->uri)
{
 $this->request->redirect($clean_url);
}

我想知道如何做同样的事情,但使用 .htaccess。

提前谢谢你。

【问题讨论】:

  • 只是一个挑剔,str_replace() 可能应该是带有$ 锚点的preg_replace()。您永远不知道什么时候可能需要 URL 中的子字符串。

标签: php .htaccess kohana


【解决方案1】:

试试这个规则:

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

此规则将从 URL 路径中删除任何前导 /index

【讨论】:

    【解决方案2】:

    未经测试,但我怀疑这应该可行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2019-04-30
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多