apache - 将 301 从目录重定向到单个文件

标签 apache .htaccess redirect directory http-status-code-301

我在弄清楚应该很简单的事情时遇到了一些麻烦。我想 301 将目录中的所有内容重定向到新位置的单个文件。

在我的 .htaccess ,我已经尝试过以下...

Redirect 301 /myDir/ http://www.mydomain.com/myNewDir/index.html

和这个...
Redirect 301 /myDir/ http://www.mydomain.com/myNewDir/

和这个...
Redirect 301 /myDir http://www.mydomain.com/myNewDir

问题是每个文件都只是映射 /myDir/ 中的每个文件,并将其附加到目标 URL 的末尾。

谷歌搜索后,我看到了一些说要做到这一点的东西......
Redirect 301 ^/myDir(.*) http://www.mydomain.com/myNewDir

但这只是做同样的事情……它将现有文件位置映射到 URL 的末尾。

在网上很容易找到很多“.htaccess 重定向”教程,但它们似乎只显示了明显的例子,如“一对一文件映射”或“一对一目录映射”。这些教程似乎也忽略了解释各种相关的文件指令以及如何正确使用它们。

这个特定的主机帐户是垃圾,还安装了 FrontPage 扩展。 Mod-rewrite 失败(破坏整个站点)但 Redirect 301线路运行良好。因此,在我可以将这个新的(非 FrontPage)站点移动到更强大的托管帐户之前,我需要坚持使用 Redirect 301单线。

我怎样才能简单地使用 Redirect 301重定向 一切/myDir/同一个文件 位于 /myNewDir/index.html ? (如果可能的话,我更喜欢只使用 /myNewDir/)。请详细解释您的解决方案中使用的文件指令。

更新:

以前接受的答案不起作用。

例子:
RedirectMatch 301 /myDir1/(.*) http://mydomain.org/newpath/myDir1/index.html

...给出了“尝试打开时发生了太多重定向”错误。

这是因为 /myDir1/(.*)正在匹配 任何地方 在字符串中,所以如果目标 URL 包含 /myDir1/任何地方,不仅仅是根,它都会被重定向到一个讨厌的循环中。

请参阅我自己发布的答案以获取正确的解决方案。

最佳答案

我在我的一个旧项目中找到了答案。
Redirect 301这都是错的。我真的很想要RedirectMatch 301反而。

RedirectMatch 301 ^/myDir/(.*) http://www.example.com/myNewDir/

说明:

http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirectmatch

"This directive is equivalent to Redirect, but makes use of standard regular expressions, instead of simple prefix matching."



http://www.zytrax.com/tech/web/regex.htm

"The ^ (circumflex or caret) outside square brackets means look only at the beginning of the target string, for example, ^Win will not find Windows in STRING1 but ^Moz will find Mozilla."



和...

"The . (period) means any character(s) in this position, for example, ton. will find tons, tone and tonneau but not wanton because it has no following character."



和...

The * (asterisk or star) matches the preceding character 0 or more times, for example, tre* will find tree (2 times) and tread (1 time) and trough (0 times).

关于apache - 将 301 从目录重定向到单个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7780727/

相关文章:

Apache shiro 属性 'sessionManager.globalSessionTimeout' 不存在

设计excel文件的java android错误

java - Web 服务 - Tomcat - 代理后面 - wsdl 位置

java - 使用我的应用程序连接到 php .htaccess

node.js 集群,将子级标准输出重定向到文件中断数据

.htaccess - 将 pdf 的外部请求重定向到嵌入 pdf 的页面

node.js - 如何在 CentOS 上运行的 Apache HTTP 服务器上设置 Loopback nodeJS 后端

php - WordPress http站点登录后一直切换到https

每个页面上的 PHP 函数导致重定向循环

javascript - jQuery 加载跟踪像素并在重定向之后