This post might be overly technical, but hey, there were no good explanation on the web, and I think I’ve understood the issue now.

The Problem: You’ve built your website somewhere else, now you want it to show under your own domain.

Using the Domain Name Service – CNAME Redirects

If you built your website using one of the online content management systems (e.g. wordpress.com, tumblr.com, weebly.com, Google Sites), you can usually easily set up a custom domain name in two simple steps:

  1. Register your domain name in the backend (see screenshot)
  2. Set up a CNAME record on your own domain, pointing to the respective web server.

The nice thing about this option: all subdirectories remain valid, i.e. onsubject.weebly.com/blog becomes www.onsubject.eu/blog. Neat, I thought: Let’s apply the same principle to my own sites! Let’s make client1.onsubject.eu into www.client1.org!

Turns out, that isn’t possible. Let me explain:

  • The CNAME record resolves the target URL into the corresponding IP address (e.g. 145.12.14.63) and sends http requests its way.
  • The host at that IP address then needs to react. For that, it consults the virtual hosts table in its web server configuration. This table defines the corresponding local directory for the website – and, unless you’re managing your own web server, it won’t be accessible for you. D’oh.

Using the Web server Settings – .htaccess

Now, in the case that both websites are on the same server (e.g. you want blog.mydomain.com to become www.mydomain.com, or www.mydomain/blog to become blog.mydomain.com), you can place a .htaccess file into the root directory:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*oldwebsite\.com\.?(:[0-9]*)?$ [NC]
RewriteRule ^/(.*)$ http://www.preferredwebsite.net/$1 [R=301,L]

These rewrite rules can become pretty complicated and take into account regular expressions and variables. In order to work, your hosting company must have enabled a module called mod_rewrite in your web server (most have).

Most scripting languages allow you to redirect web pages using a similar technique (301 redirects).

Redirecting a single page – Refresh meta tag

If you have nothing else at hand, you can also forward a website using html. Just place the following into the header:

<meta http-equiv="Refresh" content="0; url=http://www.example.com/" />

If you want to keep the original domain, but show the new content – Frames (ugh)

As URL redirection is sometimes used as part of phishing attacks, modern browsers always show the real URL of the current website. The only way to circumvent this is by using an html frame inside which you load the new site:

<frameset rows="100%">   <frame src="http://www.example.com/"> </frameset>

However, this way the URL displayed remains static even if the visitor visits multiple pages on the embedded site.

A partial conclusion

For my project, this now means that I’ll need to talk to the client and arrange to add hosting to the domain we’re using.

And for you, I hope you learned a thing or two about the different ways of redirecting web pages.

Did you spot a mistake or have a tip for me? Let me know in the comments.

Geek Corner: Redirecting Websites
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *