重定向老网域通过的.htaccess新域名 [英] Redirecting old domian to new domain by .htaccess
问题描述
我在使用Apache MOD-重写moduled及其的.htaccess
文件迁移我的旧域名新域名的过程。
我们有新的领域,其中包括
I am in process of migrating my old domain to new domain using Apache Mod-Rewrite moduled and its .htaccess
file.
we have almost same structure of the new domain which includes
- 网址
- 数据库
除了域名,就像是www.oldurl.com,现在它像www.newurl.com,这是我在我的的.htaccess
文件旧域
except the domain name, like it was www.oldurl.com and now its like www.newurl.com and this is what i have in my .htaccess
file of Old domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
上面的设置似乎工作正常,只是在一种情况下,我们有几个URL在我的旧域名已经或者被删除或结构已经改变,因此在上述规则的情况下也不会干活来了解添加一些像这样在我上面
Above settings seems to be working fine except in one case, we have few URL's in my old domain which has either been removed or structure has been changed so in that case above rule will not work.i came to know about adding something like this in my .htaccess file beside what i have described above
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Redirect 301 /my-page http://www.newurl.com/your-page
我有共计20 +这样的网址,我知道我需要那些所有20+ URL映射到有新的网址,将我需要关心的,他们应该被放在文件中的任何命令。
i have total of 20+ such URL's and i am wondering do i need to map those all 20+ URL to there new URL's and will i need to take care about any order in which they should be put in the file.
我也想知道Apache将如何工作,将它看起来在每个映射的URL任何比赛?或以其他方式工作?
i am also wondering how Apache will work, will it look at each mapped URL for any match? or it works in some other way?
推荐答案
在重定向
指令将不会被绑定到的RewriteCond
的条件,并且总是重定向 /我页为 http://www.newurl.com/your-page ,另外,mod_rewrite的有precedence在启用mod_alias所以重写规则(。*)$ http://www.newurl.com/$1 [R = 301,L]
规则被应用的在的重定向
指令得到看着。但是,如果.htaccess文件坐镇的文档根目录同时在 oldurl.com 和 newurl.com 域,则重定向
指令将被应用后,浏览器被重定向到 http://www.newurl.com/my-页面,从而重定向(再次) http://www.newurl.com/your-page
The Redirect
directive won't be bound to the RewriteCond
conditions and will always redirect /my-page to http://www.newurl.com/your-page, also, mod_rewrite has precedence over mod_alias so the RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
rule gets applied before the Redirect
directive gets looked at. However, if the .htaccess file sits within the document root of both the oldurl.com and newurl.com domains, the Redirect
directive will be applied after the browser gets redirected to http://www.newurl.com/my-page, thus redirecting (again) to http://www.newurl.com/your-page
所以,无所谓什么顺序你有这些,因为mod_rewrite的被首先应用。如果您有需要重定向到新的对您的新网站20的URL,你可以列举它们各自在自己的重定向
。否则,如果你不想让浏览器重定向两次,则可以使用mod_rewrite的引擎一一列举:
So, it doesn't matter what order you have these in, since mod_rewrite gets applied first. If you have 20 URLs that need to redirect to new ones on your new site, you can enumerate them each in their own Redirect
. Otherwise, if you'd rather not have the browser get redirected twice, you can enumerate them using the mod_rewrite engine:
RewriteEngine On
# redirect the changed URLs individually
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page$ http://www.newurl.com/your-page [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page2$ http://www.newurl.com/your-page2 [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page3$ http://www.newurl.com/your-page3 [R=301,L]
# Finally, redirect everything else as-is
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
请注意这个顺序的确实的事情在这里。不必重复2条件HTTP_HOST是一种丑陋的,你也许可以得到解决,通过使用SKIP,但它可能是更好的只是重复它们。但是,如果你有机会到你的服务器配置或虚拟主机的配置,看看在的 RewriteMap指令指令,它允许您创建一个映射,在你的情况下,旧的URL到新URL,并且可以减少所有单个更改URL重写单个之一:
Note that order does matter here. Having to repeat the 2 Conditions for HTTP_HOST is kind of ugly, you can maybe get around that by using the SKIP, but it's probably better to just repeat them. But if you have access to your server config or vhost config, take a look at the RewriteMap Directive, which allows you to create a mapping of, in your case, old urls to new urls and you can reduce all the individual changed url rewrites to a single one:
在你的服务器/虚拟主机的配置,这样的事情:
Inside your server/vhost config, something like this:
RewriteMap newurls txt:/path/to/file/map.txt
凡 /path/to/file/map.txt 看起来是这样的:
my-page your-page
my-page2 your-page2
my-page3 your-page3
etc...
和你结合的规则将如下所示:
And your combined rules would look like:
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^(.*)$ http://www.newurl.com/${newurls:$1} [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
这篇关于重定向老网域通过的.htaccess新域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!