First of all, you need to have both domains pointing at your site, but that's a HTTP server config issue I assume you have figured out already.
There are 2 parts to it, serving proper content and displaying proper links.
Serving proper content
It isn't exactly Drupal only thing. To serve content of a "subdirectory" when subdomain is called, you need to put something like this at the beginning of your htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog.example.com [NC]
RewriteRule ^/(.*)$ /blog/$1 [L]
With nginx, it would look like this (note: not my personal experience, taken from here):
server {
listen 80;
server_name blog.example.com;
location / {
proxy_pass http://127.0.0.1/blog$request_uri;
proxy_set_header Host example.com;
}
}
You could also use hook_url_inbound_alter
, but it would probably be slower than proper webserver configuration. Drupal uses webserver's URL rewriting anyway.
Revriting URLs
General way would be to use hook_url_outbound_alter()
. If you don't want hook's slowdown, you can:
- URL links to nodes, generated by Views are easy to fix - you can use "rewrite field" option to manually create links you need
- Links to theme elements and other files are bit harder to do - basically you need to create exception in rewrite rules above, and for everything that is a static file or directory omit rewriting. Example how to do this is to be found in original config, as it is the same condition that makes static files not to be processed via index.php.
- Links inside nodes - This section awaits further research