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.

An improvement, and another use for the safetags function

A while ago I wrote a function which checks HTML snippets and ensures that tags which should be closed are removed unless they are actually closed. For example, if user types

<div style="whatever">Blah Blah

and forgets to close the div tag, then that div tag will be removed. Sure it would be better if the div tag was fixed, but that requires more code. This is good enough for me.

Another use for this function is excerpts. An original article may have all the tags properly closed but the excerpt might have an opening tag with it’s matching tag cut off.

A copy of the function is here.

It is not a validator. It ignores tags like p and br which will work even if not coded properly. You can edit the function to change which tags are allowed.

I used the function to alter Virginia CURE’s website from the original cover-wp theme. For comparison, I found a couple of other websites which use cover-wp: NELA Lives, Hedge Futures, and Marco Luthe. I’m not familiar with these websites, I just looked for websites that use the cover-wp theme. Eventually, any of these websites might disappear, or any of them might change themes, including Virginia CURE. But as I’m writing this they’re good examples. Virginia CURE’s excepts have the paragraphs preserved, the others don’t. The cover-wp theme uses a customized get_the_excerpt function, which in turn calls the built-in php function striptags to remove all of the tags.

Here’s copy of functions.php which I altered from cover-wp. I renamed it functions.txt so it can be viewed. I also made a small change to get_the_excerpt.

If anyone who reads this knows of a better function then I’d be interested. I’m just an amateur hack. But for now, my function works better than anything I found.

I fixed a couple of problems since I first posted it.

PHP function to validate HTML tags for user input fields.

I have some forms with text areas that take user input. My forms aren’t as fancy as the editor that wordpress provides. I want to allow users to add html tags, but need to make sure they close their div’s and other tags which might screw up the whole page if they forget to close them. And I want to control which tags I allow. So I wrote a function that uses the htmlentities function to disable all markup and then, for those tags that I want to allow, does some checking and converts them back into enabled markup.

The function won’t fix broken markup but it will isolate broken markup so it doesn’t affect the rest of the page.

I’ve only had this function running for a day and I can’t promise I have the bugs all worked out. But I think it’s good. Please let me know if you see or find any problems with it.

I’ll copy the function below so you can get a quick view of what it does, but I’m having trouble getting wordpress to display it correctly so I put the source code in a plain text file http://www.suffolkian.com/bnmng_old/blog_files/bnmngcomputing/function_safetags.txt. Of course, you’ll have to put it in php file to make it work.

function safetags ($text) {

#This function uses htmlentities to disble markup, then converts those tags that are allowed and properly formatted back into enabled markup.

$loner_tags=array(‘br’, ‘hl’, ‘img’, ‘p’ ); #These are tags that can left unclosed. But it’s OK if they are closed.
$closed_tags=array(‘a’, ‘b’, ‘center’, ‘div’, ‘left’, ‘li’, ‘right’, ‘span’, ‘ul’ ); #These are the ones that have to be closed.

$i=0;
$pattern=array();
$replace=array();

#convert the entire text to html entities. This takes all tags and quotes and converts them to harmless strings.
$text=htmlentities($text);

#for all allowed tags, if they have closing tags then enable them.
$all_tags=array_merge($loner_tags, $closed_tags);

foreach($all_tags as $tag) {
$pattern[]=’/(?<![])<' . $tag . '((?<=' . $tag . ')s+.*?)*(?<![])>(.*?)(?<![])</' . $tag . '(?<![])>/si';
$replace[]='2′;
}

#Enable those tags that don’t have closing tags and are allowed without them.
foreach($loner_tags as $tag) {
$pattern[]=’/(?<![])<' . $tag . '((?<=' . $tag . ')s+.*?)*/?(?<![])>/si';
$replace[]='’;
}

#Within each properly converted tag, convert the properly quoted attributes.
$pattern[]=’/<([^]*?)"(.*?)"([^]*?)>/’;
$replace[]=”;

$pattern[]=’/<([^]*?)'(.*?)'([^]*?)>/’;
$replace[]=”;

#Here’s where the work gets done

for($i=0;$i<count($pattern);$i++) {
while(preg_match($pattern[$i], $text, $matches) ) {
$text=preg_replace($pattern[$i], $replace[$i], $text);
}
}

return $text;
}

Update (2010 01/03): I’m amazed whenever I get hit. But the one person who happened to stumble upon my blog this month also managed to hit a broken link. Oh well. The link’s fixed now.