Your code redirects .co.uk to .com but not when there is an appended port number. It also fails to redirect non-www to www for .com requests, with or without a port number.
RewriteBase / is the default, so does not need to be specified here.
Literal periods in RegEx patterns must be escaped.
The target URL is a literal URL and so no escaping is needed.
Mass redirects from one domain to a single URL on another domain are frowned upon. Google WebmasterTools now flags these as "Soft 404 Errors" in the Crawl Errors reports. Redirect code also amended to preserve the original part part of the request in the redirect.
What is supposed to happen if user requests example.co.uk/somepage here? Does that serve content, a 404 error, or should it redirect to .com now? I have set it to redirect to .com with the same path. If you really do need it to redirect to the root then remove the $1 from the code below. If only the root of .co.uk should redirect to the .com then change the (.*) pattern to !. (!. means "blank") as well, like this:
Code:
ErrorDocument 404 /index.php?main_page=page_not_found
Options +FollowSymLinks
RewriteEngine On
#Redirect .co.uk to .com, but should you redirect all
paths or just the root? Should you use (.*) or .! here?
# Preserve path in redirect or not? Use $1 or not use $1 here?
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Redirect non-www on .com requests to www on .com
# Redirect MUST preserve originally requested path with (.*) and $1
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Rewrite /blog URL requests to blog script internal filepath.
RewriteRule ^blog(.*)$ /index.php?main_page=blog&$1 [E=VAR1:$1,QSA,L]
I much prefer this:
Code:
ErrorDocument 404 /index.php?main_page=page_not_found
Options +FollowSymLinks
RewriteEngine On
# Redirect any requested non-blank hostname that is not
# *exactly* www.domain.com to www.domain.com here.
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Rewrite /blog URL requests to blog script internal filepath.
RewriteRule ^blog(.*)$ /index.php?main_page=blog&$1 [E=VAR1:$1,QSA,L]
I am not sure your final rewrite will work as you expect.