Redirecting to Your Main Site With Heroku

larryprice

Larry Price

Posted on August 21, 2018

Redirecting to Your Main Site With Heroku

We have a lot of domains that we want to redirect to the same server, but we use a DNS service that does not allow doing a domain forward, and we’re not allowed to upgrade. I wanted to do this in the simplest way possible, so I created a workaround using a PHP script and Heroku. The source discussed in detail in this post is available on GitHub: https://github.com/larryprice/simple-heroku-redirect-app.

The goal here is for users to visit a page and then be immediately redirected to the new site. I’ve defined two environment variables to be used in this project: SITENAME, a human-readable name for our website, and SITEURL, the full URL that we actually want the user to end up on. I’ve defined a PHP file called index.php:

<!DOCTYPE html>
<html>
  <head>
    <title><?php echo getenv('SITENAME') ?> - You will be redirected shortly...</title>
    <meta http-equiv="refresh" content="0;URL='<?php echo getenv('SITEURL') ?>'" />
  </head>
  <body>
    <p>Please visit the official <?php echo getenv('SITENAME') ?> site at <a href="<?php echo getenv('SITEURL') ?>"><?php echo getenv('SITEURL') ?></a>.</p>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

The important piece here is the <meta> tag, which actually does the redirect for us. The only PHP code here are echo getenv commands that render our environment variables in the template. Since I’m a PHP novice, there may be a better way to do this, but the echo works just fine.

We also need to tell Apache how to serve the application. We want to match any routes and render our index.php. So we create an .htcaccess file:

RewriteEngine on
RewriteRule . index.php [L]
Enter fullscreen mode Exit fullscreen mode

To satisfy Heroku, we need to list the dependencies for our PHP application. Fortunately for us, we don’t have any dependencies that Heroku does not provide by default. We’ll just create a composer.json file in the root of our project with an empty object:

{}
Enter fullscreen mode Exit fullscreen mode

That’s everything we need. You could recreate the project, but you could also just pull down the project listed above and push it up to Heroku:

$ git clone https://github.com/larryprice/simple-heroku-redirect-app.git
$ cd simple-heroku-redirect-app
$ heroku create
$ git push heroku master
Enter fullscreen mode Exit fullscreen mode

With your application available on Heroku, we still need to set the environment variables described earlier as config variables:

$ heroku config:set SITENAME=yourgooddomain.com
$ heroku config:set "SITEURL=Your Good Domain's Website Name"
Enter fullscreen mode Exit fullscreen mode

Now tell Heroku all the domains that will be accessing this application. These are the domains you want users not to use:

$ heroku domains:add yourbaddomain.com
$ heroku domains:add www.yourbaddomain.com
Enter fullscreen mode Exit fullscreen mode

Now you just need to add the records indicated by the above command to your DNS records. These will probably be CNAME records pointing from @ to yourbaddomain.com.herokudns.com or www to yourbaddomain.com.herokudns.com.

💖 💪 🙅 🚩
larryprice
Larry Price

Posted on August 21, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related