首页 > 解决方案 > htaccess 301 仅重定向根目录,但排除所有文件和子文件夹,但有例外

问题描述

我不是.htaccess代码程序员,我阅读了其他相关帖子但不理解它们。他们都不做我需要的。

我有一个运行的 Wordpress 网站,http://example.com/main但想重定向http://example.comhttp://otherexample.com/page.php. 也需要http://example.com/anyfile不被重定向。

标签: wordpress.htaccessredirect

解决方案


假设example.comotherexample.com指向不同的地方,然后在根.htaccess文件中尝试以下内容example.com

RedirectMatch 302 ^/$ http://otherexample.com/page.php

更新:我发现secondary.com 也被重定向。

如果您有多个域指向同一个地方,并且您只想重定向其中一个,那么您需要在重定向之前先使用 mod_rewrite 检查主机名。

例如,以下内容需要放在 WordPress 前端控制器之前.htaccess的文件顶部:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^$ http://otherexample.com/page.php [R=302,L]

请注意,该RewriteRule 模式匹配 URL 路径减去斜杠前缀,因此是正则表达式^$(与RedirectMatch指令不同)。


推荐阅读