regex - 将旧域重定向到新域的主页

标签 regex apache .htaccess redirect seo

我想将旧域上的所有内容(但不是主页,因为仍有内容)重定向到新域的主页。
重要提示:但我不想重定向旧主页。只是其他子文件夹,如 domain.com/*
这该怎么做?我认为这段代码行不通。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond% {HTTP_HOST} ^ olddomain.com $ [ALEBO]
RewriteRule (. *) $ Http://www.newdomain.com/$1 [R = 301, L]
</ IfModule>

最佳答案

您可以在站点根目录 .htaccess 中使用此规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule . http://www.newdomain.com%{REQUEST_URI} [R=301,L,NE]
您的规则或条件中的任何地方都没有空格。也是 RewriteRule 中的一个点确保匹配除登陆页面之外的任何内容。

关于regex - 将旧域重定向到新域的主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62750706/

相关文章:

regex - 删除文本文件中与大量短语中的任何短语匹配的行

regex - 用于货币的 Linux sed 正则表达式

python - 在 Django 和 Apache 中使用 WSGI 时如何修复 "Timeout when reading response headers from daemon process"错误

python - 使用 Apache 运行 Flask 时打印件去了哪里?

php - 防止使用浏览器 url 直接访问图像

wordpress - 为什么 WordPress 允许在不触发 404 的情况下在 url 中使用多个 page-slug?

php - 我的 .htaccess 有什么问题吗?仍尝试将文件作为 Controller 加载。自定义 MVC PHP

javascript - 正则表达式替换字符 (@),但仅在短语末尾

regex - Lua gsub - 如何在正则表达式模式中设置最大字符限制

c++ - 如何在 Apache 中托管 C 程序?