我已经将我的旧网站移动到新域,但谷歌仍然显示我以前的链接,所以我制作了一个 htAccess 文件并进行了 301 重定向。以下是我的访问代码:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old.com$
RewriteRule (.*)$ http://new.com/$1 [R=301,L]
它正在将旧网址重定向到新网址,但它不是逐页执行。意味着如果我要 www.old.com/about.php 那么它会将我重定向到 www.new.com/但我希望 www.new.com/about.php 出现。
你能建议我如何用最少的工作使用适当的正则表达式来做到这一点吗?
这将维护所有内容 - 查询字符串和请求路径:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old.com$
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L,QSA]
你根本不需要使用mod_rewrite。只需使用:
<VirtualHost *:80>
ServerName old.com
ServerAlias www.old.com
Redirect permanent / http://new.com/
</VirtualHost>
Redirect
指令会自动保留/
之后的任何内容。