简体   繁体   English

自动将所有链接从一个基本 URL 重定向到另一个?

[英]Redirect automatically all links from one basic-URL to another?

I have the following problem: We have a website that has multiple hrefs for images - They all use the same domain.我有以下问题:我们有一个网站有多个图像 hrefs - 它们都使用相同的域。 Which is something like:这就像:

<a href="http://img.example-domain.com">Avatar</a><br/>

But the server this domain is using is currently inactive, but it'll come back at some point, I just do not know when.但是这个域正在使用的服务器目前处于非活动状态,但它会在某个时候恢复,我只是不知道什么时候。

Now, we have a new image server, that is hosted on AWS.现在,我们有了一个新的图像服务器,它托管在 AWS 上。 But since the code has many entries using the old domain, I would like to know how can I redirect ALL requests to that domain to another one, instead of editing the code itself.但是由于代码有许多使用旧域的条目,我想知道如何将对该域的所有请求重定向到另一个域,而不是编辑代码本身。

For example Each request from "img.example-domain.com", shall be redirected to "img.example-domain.com.amazon.etc"例如来自“img.example-domain.com”的每个请求都应重定向到“img.example-domain.com.amazon.etc”

Please, how can I do this?请问,我该怎么做?

I am using Apache and would prefer a solution that involves using Apache Rewrite module or something like that... But if it is not possible, I will be OK too with a code solution.我正在使用 Apache 并且更喜欢涉及使用 Apache 重写模块或类似模块的解决方案......但如果不可能,我也可以使用代码解决方案。

Thanks in advance, to everyone.在此先感谢大家。

You may add this to your .htaccess -->您可以将此添加到您的 .htaccess -->

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^img.example-domain.com$ [OR]
  RewriteCond %{HTTP_HOST} ^www.img.example-domain.com$
  RewriteRule (.*)$ http://www.img.example-domain.com.amazon.etc/$1 [R=302,L]
</IfModule>

Please note: the [R=302,L] only applies to "temporary" redirects, for a permanent solution, replace with [R=301,L]请注意: [R=302,L]仅适用于“临时”重定向,对于永久解决方案,请替换为[R=301,L]

you need to setup a new server to your domain that's redirects paths to the new one您需要为您的域设置一个新服务器,该服务器将路径重定向到新服务器

you can do it with a simple node.js server like this你可以用这样一个简单的 node.js 服务器来完成

const http = require("http");
const port = process.env.PORT || 8080;

const server = http.createServer((req, res) => {
  const path = req.url;
  res.writeHead(302, { "Location": "http://new-domain.com" + path });
  res.end();
});

server.listen(port);

Note : don't forget to change http://new-domain.com to the real new domain注意:不要忘记将http://new-domain.com更改为真正的新域

Edited已编辑

You can even do it simply by using a free web host like heroku with this node.js server and linking the host with the old domain您甚至可以简单地通过使用免费的 web 主机(例如heroku)和此 node.js 服务器并将主机与旧域链接来完成此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM