wordpress - htaccess 301 仅重定向根目录,但排除所有文件和子文件夹,但有例外
问题描述
我不是.htaccess
代码程序员,我阅读了其他相关帖子但不理解它们。他们都不做我需要的。
我有一个运行的 Wordpress 网站,http://example.com/main
但想重定向http://example.com
到http://otherexample.com/page.php
. 也需要http://example.com/anyfile
不被重定向。
解决方案
假设example.com
并otherexample.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
指令不同)。
推荐阅读
- python - Selenium 单击下一步按钮会产生 selenium.common.exceptions.ElementClickInterceptedException
- python - 初学python3关于elif语句语法和print_function使用的问题
- python - 在嵌入式绘图图表上启用滚轮缩放
- r - 无法在 WSL 2 中运行的 Ubuntu 20.04 上安装 R
- typescript - 打字稿使用编译器选项“--downlevelIteration”以允许在尝试传播时迭代迭代器错误
- saxon - 使用 saxon & XPath 3.1 解析 JSON 文件
- javascript - 根据变量编号索引值添加数组
- zsh - 在 Powerlevel10k 中更改颜色
- typescript - 类型“T”上不存在属性“id”。(2339)打字稿泛型错误
- php - 在 php 中读取 google sheet json 响应中的数组