【问题标题】:Redirecting the content of a directory, but not the directory itself as well as some of its subdirectories and files重定向目录的内容,但不重定向目录本身以及它的一些子目录和文件
【发布时间】:2016-02-16 09:35:30
【问题描述】:

由于我们将配置文件的 URL 从 example.com/category/profile/ 简化为 example.com/profile 等(包括自动删除所有尾部斜杠 - 请参阅 Vanity URLs without trailing slashes),我们现在需要重定向所有绑定到旧网址。我们找到了一种使用以下代码的方法(其中还排除了我们需要保留的文件和目录)。

RewriteCond %{REQUEST_URI} !^/category/index [NC] #Excludes /category/index.html from redirect
RewriteCond %{REQUEST_URI} !^/category/images [NC] #Excludes /category/images/ from redirect
RewriteCond %{REQUEST_URI} !^/category/menu [NC] #Excludes /category/menu/ from redirect
RewriteRule ^category/(.+[^/]) /$1 [R=301,NC,L]

但这只有在我们保留所有包含我们个人配置文件的原始目录时才有效;如果我们删除这些目录,重定向将不再起作用。我们尝试了以下方法,但没有帮助。

RewriteCond %{REQUEST_URI} !^/category/index [NC]
RewriteCond %{REQUEST_URI} !^/category/images [NC]
RewriteCond %{REQUEST_URI} !^/category/menu [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^category/(.+[^/]) /$1 [R=301,NC,L]

我们正在寻找使用 RedirectMatch(下面的代码)执行此操作的替代方法(并且更简单),但我们没有找到排除需要保留的目录的方法,这也没有删除任何尾部斜杠原始的入站链接。

RedirectMatch 301 ^/category/(.*)$ /$1

【问题讨论】:

    标签: .htaccess mod-rewrite redirect httpd.conf


    【解决方案1】:

    试试:

    RedirectMatch ^/category/((?!index|images|menu|foo|bar)[^/]+)/?$ /$1
    

    【讨论】:

    • 嘿@Starkeen——你是个天才——这成功了!我们将立即在整个 FASHION NET 中实现这一点。谢谢你!!!!
    猜你喜欢
    • 2022-01-22
    • 2016-03-18
    • 2012-12-30
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多