Wordpress Spread Over Multiple Domains
I’m using Wordpress to power this site, as should be fairly obvious. However, I really like using separate sub-domain names instead of path components for various sections of a site. Aka, instead of verselogic.net/blog, I use blog.verselogic.net. However, I also wanted the top level wordpress-driven front page, and all static pages, to be located below the verselogic.net urlspace. There would be several sub-directories like verselogic.net/example, but no static files in the root.
First, I setup wordpress running on the blog subdomain as per normal. Then, I copied Wordpress’ index.php over to the root domain, changing the require() relative path to the wordpress install’s wp-blog-header.php.
So now the full wordpress tree is available on both domains, if using ugly index.php? urls. I used a little mod_rewrite magic to clean that up. For the root domain, I added several rewrite rules to apache. This could have been done with .htaccess as well:
- httpd.conf or .htaccess for root domain
- 1. RewriteRule ^files/ - [L]
2. RewriteRule ^example/ - [L]
3. RewriteCond %{REQUEST_FILENAME} !-f
4. RewriteRule ^ /index.php
The first two lines handle requests for certain sub-directories, using mod_rewrite’s Last option to stop checking rules and returning the requested resource. For these two resources, I wanted to utilize Apache’s built in directory Indexes.
If the full requested path is not one of those two directories, nor does not resolve to a file (it would 404), then line four causes a redirect to wordpress’ index.php, which further handles the static page request.
Ok, so now all wordpress pages are available on both domains, assuming that there’s no collision with a static file by the same name.
Now, I needed to convince Wordpress to write out static page links with the proper domain. This turned out to be a fairly simple plugin.
- wp-staticpagedomains.php
- function verselogicwprewritepage($id) {
return str_replace( get_settings(’home’), ‘http://verselogic.net’, $id );
}
add_filter(’page_link’, ‘verselogicwprewritepage’ );
So now static page links in wordpress will be specified with the root domain, and the remainder of the internal links (archives, posts, categories, etc) remain under the blog subdomain.
No attempt is made to actually enforce canonicallity; both domains continue to serve all wordpress resources. It would be possible to use a handful of more complex rewrites and 301 redirects to ensure viewers actually used the desired urls.
UPDATE: Wordpress 2.3 gained a Canonical Url feature. This doesn’t affect the method I used to change the Page Permalinks, but it does redirect those pages, effectively undoing this change. However, we can take advantage of the new feature to enforce our desired canonically.
- wp-staticpagedomains.php
- function verselogicmovehome($url) {
if( is_page() ) {
$url = str_replace( ‘http://blog.verselogic.net’, ‘http://verselogic.net’, $url );
}
return $url;
}add_filter(’option_home’, ‘verselogicmovehome’ );
Pages will have their canonical permalinks crafted using the shorter domain, and Wordpress will 301 redirect the user-agent to the proper url.
About this entry
You’re currently reading “Wordpress Spread Over Multiple Domains”, an entry on VerseLogic
- Published:
- 07.28.06 / 8pm
- Category:
- Words
No comments
Jump to comment form | comments rss [?] | trackback uri [?]