htaccess 301 仅重定向根目录,但排除所有文件和子文件夹但有例外 [英] htaccess 301 redirect root only but exclude all files and subfolders with exceptions

查看:95
本文介绍了htaccess 301 仅重定向根目录,但排除所有文件和子文件夹但有例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有一个在 http://example.com/main 上运行的 Wordpress 网站,但想将 http://example.com 重定向到 http://otherexample.com/page.php.还需要 http://example.com/anyfile 不被重定向.

解决方案

假设 example.comotherexample.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).

这篇关于htaccess 301 仅重定向根目录,但排除所有文件和子文件夹但有例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆