【问题标题】:Redirect all urls with subdirectories to root except two将所有带有子目录的 url 重定向到根目录,除了两个
【发布时间】:2012-06-10 19:30:39
【问题描述】:

我最近更改了我的一个网站的链接结构,需要进行 301 重定向:

所有带有子目录的链接都指向没有子目录的版本(即只是让子目录消失)

例如

domain.com/fdfddffd/test -> domain.com/test
domain.com/yipeee/test -> domain.com/test
domain.com/fdfddffd/aaa -> domain.com/aaa etc.

所以我考虑使用

RewriteRule  ^([^/]+)(.*)$     / [R]

但我需要从中排除两个目录,一个是“搜索”,另一个是“数据”。如何将上述重写规则与排除相结合?

【问题讨论】:

    标签: php .htaccess exception redirect


    【解决方案1】:

    使用RewriteCond 排除这两个表达式,如!^/(search|data)/

    # Unless the request matches /search/ or /data/
    RewriteCond %{REQUEST_URI} !^/(search|data)/
    # Rewrite to the contents of the second group (.*)
    RewriteRule  ^([^/]+)/(.*)$  /$2 [L,R=301]
    

    【讨论】:

    • 正是我想要的。谢谢你:)
    猜你喜欢
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    相关资源
    最近更新 更多