重定向目录的内容,但不重定向目录本身及其某些子目录和文件 [英] Redirecting the content of a directory, but not the directory itself as well as some of its subdirectories and files

查看:42
本文介绍了重定向目录的内容,但不重定向目录本身及其某些子目录和文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着我们将配置文件的URL从example.com/category/profile/简化为example.com/profile等(包括自动删除所有结尾的斜杠-参见

As we simplified the URLs of our profiles from example.com/category/profile/ to example.com/profile, et cetera (including the automatic removal of all trailing slashes -- see Vanity URLs without trailing slashes), we now need to redirect all inbound links bound for the old URLs. We found a way to do this with the following code (which also excludes the files and directories we needed to keep).

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]

但是,只有当我们保留包含我们的个人资料的所有原始目录时,此方法才有效;如果我们删除这些目录,则重定向将不再起作用.我们尝试了以下方法,但这没有帮助.

But this only works if we keep all the original directories that contain our individual profiles; if we delete these directories, the redirect no longer works. We tried the below, but that didn't help.

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(下面的代码)执行此操作的一种替代方法(并且更简单),但是我们没有找到一种方法来排除需要保留的目录,并且也没有从中删除任何尾部斜杠.原始的入站链接.

We were looking into an alternative (and much simpler) way to do this using RedirectMatch (code below), but we did not find a way to exclude the directories we need to keep and this also did not remove any trailing slashes from the original, inbound links.

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

推荐答案

尝试:

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

这篇关于重定向目录的内容,但不重定向目录本身及其某些子目录和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆