【问题标题】:.htaccess redirect that remove certain strings.htaccess 重定向删除某些字符串
【发布时间】:2016-02-25 18:15:29
【问题描述】:

我有一个框架可以从中加载 index.php:

website.com
website.com/index
website.com/index/
website.com/index/index
website.com/index/other
website.com/index/some/page/that/not/exists

因为控制器“索引”存在,如果它没有找到任何功能,则加载索引。 其他子页面也一样:

website.com/realpage/
website.com/realpage/index
website.com/realpage/index/some/page/that/not/exists

那么,.htaccess 可以检查 url 是否包含任何“/index”或“index”并删除其后的所有内容? 我还需要在 url 的末尾强制没有斜杠。

我想要的结果:

website.com/index                           -> website.com
website.com/index/                          -> website.com
website.com/index/index                     -> website.com
website.com/index/other                     -> website.com
website.com/index/some/page/that/not/exists -> website.com

website.com/realpage/                                -> website.com/realpage
website.com/realpage/index                           -> website.com/realpage
website.com/realpage/index/some/page/that/not/exists -> website.com/realpage

谢谢!

@Rafael Saraiva 我试过这个: 重写规则 ^(.*)/index$ $1 [L]

它现在向我显示 404:

website.com/index/index
website.com/index/other
website.com/index/some/page/that/not/exists

正确,这些页面不存在。但仍然显示主页:

website.com/index
website.com/index/

我想在这里重定向到“website.com”。

【问题讨论】:

    标签: regex apache .htaccess


    【解决方案1】:

    排除特定语法的通用正则表达式将是:

    (.*)index(.*)\/index

    要测试正则表达式,请使用this site. 要进一步检测它是否是有效的网址,您需要使用不同的正则表达式。

    【讨论】:

    • 我已经尝试过:RewriteRule ^(.*)/index$ $1 [L] 它现在显示 404 在:website.com/index/index website.com/index/other website .com/index/some/page/that/not/exists 正确,这些页面不存在。但仍然显示主页: website.com/index website.com/index/ 我想在这里重定向到“website.com”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多