Redirecting a Request with Path Information

bnmng.com is mapped to my share on totalchoicehosting‘s servers. But users can still type bnmng.com in the address bar and get redirected to the WordPress blog along with any extra path information. So http://bnmng.com/category/computing/ becomes https://bnmng.wordpress.com/category/computing.

I do this with a simple index.php in the home directory:
index.php
<?php
header('LOCATION: https://bnmng.wordpress.com' . $_SERVER['REQUEST_URI']);
?>

$_SERVER[‘REQUEST_URI’] is everything after the .com. In case the code wraps on your screen, this is just one line of code between the <?php and ?> tags.

But this wouldn’t work unless I ensured that my index.php file would respond to the url with the path info.

To do that, I copied WordPress.org‘s .htaccess file (described here as “genius”, and explained in more detail here), which is found on self-hosted WordPress blogs. This requires Apache’s mod_rewrite.

.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php  [L]
</IfModule>

Without this, http://bnmng.com/category/computing/ would simply return a “page not found” error.

That’s all I need to make the redirect. The redirect only occurs if the request is bnmng.com without any path information or bnmng.com with path information that doesn’t correspond to anything on my share. http://bnmng.com/codeexamples, for example, doesn’t get redirected.

This also seems to work for any bnmng.com feeds that people may have subscribed to before I moved bnmng.com from wordpress.com to totalcoicehosting.com. There’s no need to create a feed directory on totalchoicehosting, as I described in an earlier post.

I could have just put a self-hosted blog in my home directory and had a smoother effect without the redirect, but I like the convenience of using WordPress.com’s servers. And if they make a little money with ads on my blog then I’m happy to support them, especially if it doesn’t cost me a dime.

Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s