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

All we need is an easy explanation of the problem, so here it is.

I have the following problem: We have a website that has multiple hrefs for images – They all use the same domain. Which is something like:

<a href="">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.
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 "", shall be redirected to ""

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.

Thanks in advance, to everyone.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You may add this to your .htaccess –>

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^$ [OR]
  RewriteCond %{HTTP_HOST} ^$
  RewriteRule (.*)$$1 [R=302,L]

Please note: the [R=302,L] only applies to "temporary" redirects, for a permanent solution, replace with [R=301,L]

Method 2

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

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

const server = http.createServer((req, res) => {
  const path = req.url;
  res.writeHead(302, { "Location": "" + path });


Note: don’t forget to change to the real new domain


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

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply