apache .htaccess 重定向到外部 URL,同时隐藏重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1807925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.htaccess redirect to external URL while hiding redirect
提问by Ben Hall
I want to be able to redirect a domain pointed to my webhosting to an external domain.
我希望能够将指向我的虚拟主机的域重定向到外部域。
For example, I have this in my .htaccess:
例如,我的 .htaccess 中有这个:
RewriteCond %{HTTP:Host} ^(?:www\.)?mydomain\.example$
RewriteRule ^(.*)$ http://myexternal.example/site [R=301,NC]
However, when I visit the domain, the URL in my address bar changes to http://myexternal.example/site.
但是,当我访问该域时,地址栏中的 URL 更改为http://myexternal.example/site。
How can I redirect without changing the URL?
如何在不更改 URL 的情况下重定向?
Is there another way around this? Do I need to use a frame/iframe?
有没有其他方法可以解决这个问题?我需要使用框架/iframe 吗?
回答by Luc
Bit long ago, but I'll answer the question anyway for those who come here by Google (like me). The answer is really simple:
很久以前,但无论如何我都会为那些通过谷歌来到这里的人(像我一样)回答这个问题。答案其实很简单:
In your htaccess, remove the R=301 part (and the comma of course).
在您的 htaccess 中,删除 R=301 部分(当然还有逗号)。
R=301 means you do it via a 301 redirect. You don't want that
R=301 表示您通过 301 重定向来完成。你不想那样
回答by Matthew Scharley
Either a single frame frameset, or an iframe with width/height set to 100%.
单个框架框架集,或宽度/高度设置为 100% 的 iframe。
I'm not sure if framsets are supported in newer versions of HTML, but browsers still understand old versions anyway... but a single iframe is easy anyway.
我不确定新版本的 HTML 是否支持框架集,但浏览器仍然可以理解旧版本……但无论如何,单个 iframe 很容易。
<html>
<head>
<title>My Site</title>
<style>
body {
margin: 0;
padding: 0;
}
body, iframe {
width: 100%;
height: 100%;
}
iframe {
border: 0;
}
</style>
</head>
<body>
<iframe src="http://example.com" />
</body>
</html>
回答by Martin
Maybe you can achieve this by changing the DNS for your domain mydomain.net to link to myexternal.net. Then, you have to use an appropriate .htaccesson your external server.
也许您可以通过更改域 mydomain.net 的 DNS 以链接到 myexternal.net 来实现这一点。然后,您必须.htaccess在外部服务器上使用适当的。
回答by starkeen
None of the other answers suggested this. What op is looking for is mod-proxy. You can proxy the request from your domainAto domainBusing Pflag of mod-rewrite.
其他答案都没有提出这一点。op 正在寻找的是mod-proxy。您可以使用mod-rewrite 的P标志将请求从domainA代理到domainB。
RewriteRule ^(.*)$ http://domainB.com/ [P]
This will internally redirect all requests from domainA to domainB.
这将在内部将所有请求从域 A 重定向到域 B。
Make sure mod-proxyis enabled on your server.
确保在您的服务器上启用了mod-proxy。
回答by WAS Admin
I would like to suggest 2 ways how you can go about redirecting your site to another URL, without changing the domain.
我想建议两种方法,如何在不更改域的情况下将站点重定向到另一个 URL。
Example 1: Redirect and keep everything after the URL The first option will show all of the same content on one URL as you would another. For example, if you just changed your domain to DomainB.com, but you still have plenty of visitors coming to DomainA.com, you would use this to show them all of the existing content that is located on the new domain, without the need to update both websites. To do this, you would modify your .htaccess file for the domain that your users will go to, and insert these lines of code: RewriteCond %{HTTP_HOST} ^DomainA.com RewriteRule ^(.) http://DomainB.com/$1 [P] If you are using the file manager in cPanel, be sure that you have the option to show hidden files selected. What does the above redirect do? After adding this line into your .htaccess file, you will be able to go to DomainA.com/YourPage and it will show the content from DomainB.com/YourPage*
Ë xample 1:重定向和把一切都在URL后,你会另一个第一个选项会显示所有相同内容的上一个网址。例如,如果您刚刚将域更改为 DomainB.com,但仍有大量访问者访问 DomainA.com,您可以使用它向他们显示位于新域中的所有现有内容,而无需更新两个网站。为此,您需要修改用户将访问的域的 .htaccess 文件,并插入以下代码行: RewriteCond %{HTTP_HOST} ^DomainA.com RewriteRule ^(.) http://DomainB.com/$1 [P] 如果您在 cPanel 中使用文件管理器,请确保您可以选择显示隐藏文件。上面的重定向有什么作用?将此行添加到您的 .htaccess 文件后,您将能够转到 DomainA.com/YourPage,它将显示来自 DomainB.com/YourPage* 的内容
Example 2: Redirect a domain to a specific url There is another way you can do your redirect to show a specific URL, but keep the domain the same as well. If you want visitors to go to DomainA.com with a specific page in mind when doing so, you may use this code:
示例 2:将域重定向到特定 url 还有另一种方法可以使重定向显示特定 URL,但也要保持域相同。如果您希望访问者在访问 DomainA.com 时考虑特定页面,则可以使用以下代码:
RewriteCond %{HTTP_HOST} ^DomainA.com RewriteRule ^(.) http://DomainB.com/PathToPageHere[P]*
RewriteCond %{HTTP_HOST} ^DomainA.com RewriteRule ^(.) http://DomainB.com/PathToPageHere[P]*
*You would use this method if, for example, you had an external blog such as one on blogspot.com or maybe a shopping cart on etsy.com that you want people to visit your domain without fully hosting the domain there. Now, visitors can access your site using your domain, but see the content of an external URL. Example 3 Re-directing an IP address Occasionally, there will be a request to re-direct an IP address to a specific URL. The following code shows how this can be done in the .htaccess file.
*例如,如果您有一个外部博客(例如 blogspot.com 上的博客)或 etsy.com 上的购物车,您希望人们访问您的域,而无需在那里完全托管域,则您可以使用此方法。现在,访问者可以使用您的域访问您的网站,但会看到外部 URL 的内容。示例 3 重定向 IP 地址 有时,会有将 IP 地址重定向到特定 URL 的请求。以下代码显示了如何在 .htaccess 文件中完成此操作。
Redirect all IP address (replace the ## with the IP address numerals) to same http://domain_name.com
将所有 IP 地址(用 IP 地址数字替换 ##)重定向到相同的http://domain_name.com
RewriteCond %{HTTP_HOST} ^##.##.##.## RewriteRule (.) http://domain_name.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^##.##.##.## RewriteRule (. ) http://domain_name.com/$1 [R=301,L]

