如何仅通过更改域名来重定向 URL,同时保留其他 URL 参数

Kar*_*lam 5 regex migration .htaccess redirect forwarding

我现在正在将我的网站迁移到新的主机和域,我想知道是否可以将任何输入旧网站的任何 URL 的人重定向到新网站,同时保留所有 URL 参数。例如:

当有人输入这个 url 时http://www.domainA.com/blog/?p=667,我希望他被重定向到http://www.domainB.com/blog/?p=667.

有什么办法可以通过添加一些 .htaccess 配置来做到这一点吗?

谢谢!

anu*_*ava 3

在您的 .htaccess 文件中尝试以下操作:

Options +FollowSymlinks -MultiViews
RewriteEngine on

# for http
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://www.domainB.com/$1 [R=301,L]

# for https
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://www.domainB.com/$1 [R=301,L]
Run Code Online (Sandbox Code Playgroud)

这将在从域 A 重定向到域 B 时保留您的 URI。