将旧域重定向到新域 - Rewriterule

发布于 2025-01-11 08:53:27 字数 950 浏览 0 评论 0原文

以下是我的 htaccess 文件中的重定向规则。他们将 https://olddomain.com 重定向到 https://subdomain.domain.com 但网页没有被重定向。我仍然在加载 olddomain.com/page1。

`RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
 RewriteCond %{HTTP_HOST} ^www\.old\.com$ 
 RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]`

我添加了以下规则,该规则部分起作用,域后的斜杠丢失。现在重定向是 https://subdomain.domain.compage1 而不是 https://subdomain.domain.com/page1

RewriteRule ^ https\:\/\/subdomain\.domain\.com\/{REQUEST_URI} [L,R=301]

如何解决此问题。请提供任何帮助。 (我尝试了重定向,但最后没有转义 \,但这不起作用。

RewriteRule ^ https\:\/\/subdomain\.domain\.com{REQUEST_URI} [L,R=301])

Following are the redirection rule I have in my htaccess file. They redirect https://olddomain.com to https://subdomain.domain.com
but the web pages are not getting redirected. I still have olddomain.com/page1 loading.

`RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
 RewriteCond %{HTTP_HOST} ^www\.old\.com$ 
 RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]`

I added the following rule which is working partially, the slash after the domain is missing. Now the redirect is https://subdomain.domain.compage1 instead of
https://subdomain.domain.com/page1

RewriteRule ^ https\:\/\/subdomain\.domain\.com\/{REQUEST_URI} [L,R=301]

How to fix this. Any help please. (I tried the redirect without escaping \ at the end but this didn't work.

RewriteRule ^ https\:\/\/subdomain\.domain\.com{REQUEST_URI} [L,R=301])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2025-01-18 08:53:27
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$ 
RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]

这些仅重定向主页(根目录)。要将任何 URL 路径重定向到目标处的同一 URL 路径,请使用以下命令:

RewriteCond %{HTTP_HOST} ^(www\.)?old\.com [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L]

我已组合您的两个条件

请注意,引用 REQUEST_URI 服务器变量的语法是 %{REQUEST_URI}(带有 % 前缀)。否则,“{REQUEST_URI}”只是文字文本。

无需对替换字符串中的所有这些字符进行反斜杠转义。

测试前清除浏览器缓存。最好使用 302(临时)重定向进行测试,以避免潜在的缓存问题。


我添加了以下规则,该规则部分起作用,后面的斜杠
域名丢失。现在重定向是
https://subdomain.domain.compage1 而不是
https://subdomain.domain.com/page1

RewriteRule ^ https\:\/\/subdomain\.domain\.com\/{REQUEST_URI} [L,R=301]

除了上述指令不可能做到这一点之外 - 您可能会看到由于早期错误重定向而导致的缓存响应。 301(永久)重定向由浏览器永久缓存。始终使用 302(临时)重定向进行测试,最好禁用浏览器开发工具的浏览器缓存(“网络”选项卡上的选项)。


替代解决方案

这需要进入根 .htaccess 文件(如果它位于子目录 .htaccess 中,它将不起作用,与上面的规则不同)。

RewriteCond %{HTTP_HOST} ^(www\.)?old\.com [NC]
RewriteRule (.*) https://subdomain.domain.com/$1 [R=301,L]

这使用反向引用而不是 REQUEST_URI 服务器变量。

此重定向需要位于 .htaccess 文件顶部附近,位于大多数其他指令之前。

确保在测试之前已清除浏览器缓存。

RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$ 
RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]

These redirect the homepage (root directory) only. To redirect any URL-path to the same URL-path at the target use the following instead:

RewriteCond %{HTTP_HOST} ^(www\.)?old\.com [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L]

I've combined your two conditions.

Note that the syntax is %{REQUEST_URI} (with a % prefix) to reference the REQUEST_URI server variable. "{REQUEST_URI}" is otherwise just literal text.

There is no need to backslash-escape all those characters in the substition string.

Clear your browser cache before testing. And preferably test with 302 (temporary) redirects to avoid potential caching issues.


I added the following rule which is working partially, the slash after
the domain is missing. Now the redirect is
https://subdomain.domain.compage1 instead of
https://subdomain.domain.com/page1

RewriteRule ^ https\:\/\/subdomain\.domain\.com\/{REQUEST_URI} [L,R=301]

Except that that is not possible with the directive as stated - you were perhaps seeing a cached response as a result of earlier erroneous redirects. 301 (permanent) redirects are cache persistently by the browser. Always test with 302 (temporary) redirects and ideally with the browser cache disabled (option on the "Network" tab) of the browser dev tools.


Alternative solution

This would need to go in the root .htaccess file (it would not work if it is in a subdirectory .htaccess, unlike the rule above).

RewriteCond %{HTTP_HOST} ^(www\.)?old\.com [NC]
RewriteRule (.*) https://subdomain.domain.com/$1 [R=301,L]

This uses a backreference instead of the REQUEST_URI server variable.

This redirect needs to go near the top of the .htaccess file, before most other directives.

Make sure you've cleared your browser cache before testing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文