3

I need some help figuring this out. I have a main site mysite.com on Drupal 7. I want to build a blog for it and want for people to access it using blog.mysite.com. So basically it's the same site (so I can manage content from the same place), just on different URLs.

NOTE*: Individual posts page should show the URL like so: blog.mysite.com/my-post

I was wondering if this can be done using a View under the path name mysite.com/blog and mask the URL somehow to show blog.mysite.com instead. I am using nginx as a webserver.

3

Sounds like you'll need to use the Domain Access module to get this right. This module allows you to run as many affiliated sites as you need from one single Drupal installation, sharing the same database.

I'm using it for another site at the moment, and it works really well.

  • Isn't it too much? OP only wants to have one view and one content type displayed under subdomain. He does not need a whole affiliated site. – Mołot Oct 18 '13 at 14:23
  • it might be too much yes, but it is the first technique which I have had experience with which comes to mind :) I'd be really interested to see if anyone else knows an easier / more lightweight technique – Alex Kirsten Oct 18 '13 at 14:26
  • 1
    More lightweight (sadly - not easier) in my answer. – Mołot Oct 18 '13 at 14:44
3

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
  • weird there is no module for this yet.. there are modules that deal with domain stuff, but not really on this rather simple level, it seems. – donquixote Jun 6 '14 at 10:39
  • 1
    What about hook_url_inbound_alter() and hook_url_outbound_alter(), to get the links you want? – donquixote Jun 6 '14 at 10:40
  • @donquixote actually, you are right, answer updated. For module, see accepted answer - there is one, even if it's a bit of an overkill. – Mołot Jun 6 '14 at 10:53
  • Yes, domain access seems overkill, and also I don't see how it replaces example.com/blog with blog.example.com, thus stripping the path fragment. – donquixote Jun 6 '14 at 10:54
  • The url rewriting is maybe more complicated than you put it: On blog.example.com, you need to rewrite ALL urls to example.com, except when they contain /blog. On example.com, you need to rewrite links to /blog to blog.example.com. – donquixote Jun 6 '14 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.