apache 如何为所有以同一文件夹开头然后是一些但不是所有子文件夹的 url 设置 RewriteRule

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1102280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:51:56  来源:igfitidea点击:

How do I set up RewriteRule for urls that all start with the same folder and then some but not all subfolders

apachemod-rewrite

提问by Gumbo

I am trying to write a rule to redirect some but not all of the content of a certain folder:

我正在尝试编写规则来重定向某个文件夹的部分但不是全部内容:

  • ^folder1/ any .html files
  • ^folder1/blackberry
  • ^folder1/content
  • ^folder1/data
  • ^folder1/images
  • ^folder1/docs
  • ^folder1/ 任何 .html 文件
  • ^文件夹1/黑莓
  • ^文件夹 1/内容
  • ^文件夹 1/数据
  • ^文件夹 1/图像
  • ^文件夹 1/文档

I need to use RewriteRule to send everything except ^folder1/blackberry to another site (eg, http://somedomain.com/main.html) and I'm sure there must a way to do this with regular expressions but I don't (yet) know how :-)

我需要使用 RewriteRule 将除 ^folder1/blackberry 之外的所有内容发送到另一个站点(例如http://somedomain.com/main.html),我确信必须有一种方法可以使用正则表达式来做到这一点,但我没有t(还)知道如何:-)

回答by Gumbo

Use a rule to catch everything and exclude the exceptions with a RewriteConddirective:

使用规则捕获所有内容并使用RewriteCond指令排除异常:

RewriteCond %{REQUEST_URI} !^/folder1/blackberry$
RewriteRule ^folder1/ http://sub.example.com/main.html [L,R=301]

This rule redirects every request with a URL path that starts with /folder1/except /folder1/blackberryexternally to http://sub.example.com/main.html.

此规则将每个请求重定向到一个 URL 路径,该 URL 路径以/folder1/except /folder1/blackberryexternally开头http://sub.example.com/main.html