Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 将所有请求从旧域重定向到新域_Apache_.htaccess_Http Status Code 301 - Fatal编程技术网

Apache 将所有请求从旧域重定向到新域

Apache 将所有请求从旧域重定向到新域,apache,.htaccess,http-status-code-301,Apache,.htaccess,Http Status Code 301,我希望从旧域迁移到新域 我的旧域名olddomain.com和新域名newdomain.com目前指向相同的ip地址 我有Apache服务器来处理请求 我如何301重定向我所有的 olddomain.com/* & 到 我可以获得需要添加到htaccess中的确切正则表达式或配置吗 My newdomain.com和olddomain.com都由同一个apache从同一IP提供服务器,因此“/”重定向可能导致循环?我们也在寻找有效的方法 我试过了 <IfModule mod_rewrit

我希望从旧域迁移到新域

我的旧域名
olddomain.com
和新域名
newdomain.com
目前指向相同的ip地址

我有Apache服务器来处理请求

我如何
301重定向我所有的

olddomain.com/*
&

我可以获得需要添加到
htaccess
中的确切正则表达式或配置吗

My newdomain.com和olddomain.com都由同一个apache从同一IP提供服务器,因此“/”重定向可能导致循环?我们也在寻找有效的方法

我试过了

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^localhost$ [OR]
    #  RewriteCond %{HTTP_HOST} ^www.olddomain.com$
    RewriteRule (.*)$ http://comp16/$1 [R=301,L]
</IfModule>
但当我在浏览器中点击localhost,将站点重定向到我的计算机名时,它不会将站点重定向到每个
olddomain.com
主机的配置(
VirtualHost
)中的comp16

,请尝试以下操作:

Redirect permanent / http://newdomain.com/
。当所有内容都应该重定向时,这是首选的方式。如果您必须使用
模式\u rewrite/htaccess
在这方面有很多问题,其中之一是:

编辑
Apache关于以下方面的建议:


将下面的代码写入.htaccess,它会将所有旧域请求重定向到新域

RewriteEngine on
RewriteBase /
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]

我还建议使用If语句,因为您也可以在多站点服务器中使用它。仅键入:

<If "%{HTTP_HOST} == 'old.example.com'">
    Redirect "/" "https://new.example.com/"
</If>

重定向“/”https://new.example.com/"

询问谷歌会比在这里添加问题更快。;-)这将重定向新的和旧的域的所有请求到新的域,但我只需要旧的域URL重定向到新的域。这不是一个与谷歌通常问的问题不同的问题吗?当运行
mod_rewrite
时,我建议您启用
RewriteLog
以查看实际发生的情况。我觉得这里的问题是,我的新域和旧域都由同一个apache检查。这样重定向公平吗?当然,就性能而言,它比启动
mod_rewrite
和重写东西要好。但我想这主要取决于你觉得用起来舒服些什么。然而,对于这类事情的建议是在
重写规则
上使用
重定向
。这将把新域和旧域的所有请求重定向到新域,但我只需要将旧域URL重定向到新域。
Redirect permanent / http://newdomain.com/
mod_alias provides the Redirect and RedirectMatch directives, which provide a means to
redirect one URL to another. This kind of simple redirection of one URL, or a class of 
URLs, to somewhere else, should be accomplished using these directives rather than 
RewriteRule. RedirectMatch allows you to include a regular expression in your 
redirection criteria, providing many of the benefits of using RewriteRule.
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]
<If "%{HTTP_HOST} == 'old.example.com'">
    Redirect "/" "https://new.example.com/"
</If>