更新时间:2023-09-05 13:07:04
假设 example.com
和 otherexample.com
指向不同的地方,然后尝试如下example.com
中的根 .htaccess
文件:
RedirectMatch 302 ^/$ http://otherexample.com/page.php
更新:我发现secondary.com也被重定向了.
如果您有多个域指向同一个地方,而您只想重定向其中一个,那么您需要在重定向之前先使用 mod_rewrite 检查主机名.
例如,以下内容需要位于 .htaccess
文件的顶部 WordPress 前端控制器之前:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]重写规则 ^$ http://otherexample.com/page.php [R=302,L]
请注意,RewriteRule
pattern 匹配 URL-path 减去斜线前缀,因此正则表达式 ^$
(不同于 RedirectMatch
指令).
I am not a programmer of .htaccess
code, I read the other related posts but do not understand them. Non of them do what I need.
I have a Wordpress site that runs on http://example.com/main
but want to redirect http://example.com
to http://otherexample.com/page.php
. Also need that http://example.com/anyfile
not be redirected.
Assuming example.com
and otherexample.com
point to different places then try something like the following in the root .htaccess
file at example.com
:
RedirectMatch 302 ^/$ http://otherexample.com/page.php
UPDATE: I found that secondary.com also is redirected.
If you have multiple domains pointing to the same place and you only want to redirect one of them then you'll need to use mod_rewrite to check the hostname first before redirecting.
For example, the following would need to go at the top of the .htaccess
file before the WordPress front-controller:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^$ http://otherexample.com/page.php [R=302,L]
Note that the RewriteRule
pattern matches against the URL-path less the slash prefix, hence the regex ^$
(different to the RedirectMatch
directive).