Map a Separate Domain Name to a WordPress Page or Post

I had a client who wanted to map a domain name directly to a WordPress Page, and not show the WordPress URL.

For example, let’s say you had a WordPress blog with this Page:
http://myblog.com/my-first-post/

But let’s say you want the URL of that post to be http://airplanesarecool.com.

Of course, you could do a simple redirect, but that would result in http://myblog.com/my-first-post being shown as the URL in your browser, not the original domain name.

In this article, I’ll show you how to mask a WordPress page or post with a separate URL with completely different domain name.  Note, I am assuming in this article that you own the domain name that you want to use (i.e., “airplanesarecool.com”), and that it is pointing to the root of your WordPress site.

My First Try

I a not a mod_rewrite expert, but I knew the answer lay there. After a lot of reading and tinkering, I came up with this code for my .htaccess file:

This did do the redirect, but it did not mask the URL. That is, the WordPress URL was still showing up in the browser, not the desired domain name.

I Am The One That Masks

After more tinkering, I discovered that the original domain name was preserved only if I redirected to a file at the root level. It wouldn’t work for subdirectories (maybe one of you knows why?)

Armed with that knowledge, I changed my approach to redirect the URL to the raw “non-pretty” WordPress URL, which is basically index.html at the root level with a query string. Thus, I came up with this:

This works! If you type in “airplanesarecool.com” in the browser, it will display the page at myblog.com/my-first-post, but keep your original URL! Great!!!

As I mentioned, you’ll need to point your domain name at your WordPress root directory. That is, point airplanesarecool.com to myblog.com.

Next, place the code above the WordPress code in your .htaccess file at the WordPress root. That is, put it above the line that says:

In the fourth line, specify the ID of your WordPress Page or Post. You can find the ID by viewing your Page or Post in edit mode and looking at the URL. It’s in there.

How it Works

The first line simply turns on the redirecting engine, and the second sets the base directory.

The third line is where the magic starts to happen. It says, “look at the ‘HTTP_HOST’ variable and see if it matches ‘airplanesarecool.com’. The “[NC]” means the test is case-insensitive.

If this condition is met, the fourth line redirects the user to index.php?p=180, where 180 is the ID of the WordPress Page or Post. The “QSA” is needed to preserve query strings.

That’s it! Let me know how this worked for you, or if you have any questions, or if you have any corrections or improvements. I am by no means an expert in mod_rewrite, but I hope to be some day. – Brian

31 thoughts on “Map a Separate Domain Name to a WordPress Page or Post

  1. IT worked well for me for one subdomain but when i add another one ie wp_page_id and map to subdomain, it then doesn’t work. Can you please help me?

    1. Hi Biagio,

      You can leave your site_url setting to whatever the rest of the site’s URL is. The .htaccess mod will override it as far as external visitors are concerned.

      Best,
      Brian

  2. This is not working for me, I have created a subdomain for a specific page, this redirects but unable to mask the page url with the subdomain. How can I do that?

  3. I know this has been up for awhile and I understand the concepts. Seems pretty easy to implement for a single redirect. Is it possible to do this for more than one redirect? We had 4 different domain names for 4 different divisions and now we would prefer to have them all under one wordpress template each with their own page displaying their original domain. Hoping you are still willing to offer your input. Thanks

Leave a Comment or Ask a Question