Drupal và sub-domain
Đối với những site có rất nhiều nội dung, có lẽ chúng ta cần tạo những URL đơn giản, gọn gàng để người dùng dễ nhớ để truy cập lại sau này. Thí dụ, toila.net/thehong thì đẹp hơn toila.net/index.php?q=user/1, web.com/thongbao sẽ dễ nhớ hơn web.com/index.php?q=taxonomy/term/1+2+3 rất nhiều hay là thehong.yeublog.com sẽ chuyên nghiệp hơn là yeublog.com/index.php?module=blog&action=frontpage, ... Đối với sub-domain dạng sub folder thì vấn đề được giải quyết rất đơn giản, nhưng đối với sub-domain dạng *subdomain.domain.com* thì có một số trở ngại. Bài viết này nêu lên một số trở ngại và cách giải quyết.
Yêu cầu hệ thống
- Apache/server hỗ trợ mod_rewrite, cấu hình trên từng thư mục bằng tập tin
.htaccess. - Có cài đặt module rewrite.
- Host phải hỗ trợ chức năng subdomain wildcard -- subdomain ảo. Khi chức năng subdomain wildcard này được kích hoạt, các request đến server dưới dạng
*http://foo.domainname.com/bar*sẽ được server hiểu là*http://domainname.com/bar*. Khi đó, các request đến Drupal site đều quy về một mối -- các request dạnghttp://foo.domainname.com/node/1, server sẽ gọi Drupal site của bạn với URL có dạnghttp://domainname.com/index.php?q=node/1
Cookie
Việc tạo tên SESSION trong Drupal phụ thuộc vào domain (và sub domain) được request, cho nên, cũng là một người truy cập, nhưng khi bạn truy cập http://domainname.com và khi bạn truy cập http://foo.domainame.com, thì đối với Drupal, bạn là hai người khác nhau. Tham khảo hàm config_init. Để giải quyết trở ngại này, chúng ta phải đồng bộ tên SESSION khi sử dụng các domain/sub-domain. Mở tập tin sites/settings/settings.php, thêm (bỏ comment) dòng:
<?php
$cookie_domain = domainname.com;
?>Vấn đề còn lại giờ, chúng ta hải chỉnh lại tập tin .htaccess để Drupal hiểu được chính xác các yêu cầu của người dùng ứng với các domain/sub-domain:
- http://foo.domainname.com/ => http://domainname.com/index.php?q=foo
- http://bar.domainname.com/baz/blah => http://domainname.com/index.php?q=baz/blah
Rewrite
Apply patch này (áp dụng cho drupal 5.7) để điều chỉnh lại tập tin .htaccess. Khi đó, .htaccess của drupal sẽ có dạng sau:
# # Apache/PHP/Drupal settings: # # Protect files and directories from prying eyes. <FilesMatch "\.(engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(code-style\.pl|Entries.*|Repository|Root|Tag|Template)$"> Order allow,deny </FilesMatch> # Don't show directory listings for URLs which map to a directory. Options -Indexes # Follow symbolic links in this directory. Options +FollowSymLinks # Customized <a href="/taxonomy/term/162" title="">Error</a> messages. ErrorDocument 404 /index.php # Set the default handler. DirectoryIndex index.php # Override <a href="/category/t%E1%BA%A1p-ghi/php.html" title="">php</a> settings. More in sites/default/settings.php # but the following cannot be changed at runtime. # <a href="/category/t%E1%BA%A1p-ghi/php.html" title="">php</a> 5, <a href="/category/ph%C3%A2n-lu%E1%BB%93ng/apache.html" title="">Apache</a> 1 and 2. <IfModule mod_php5.c> php_value magic_quotes_gpc 0 php_value register_globals 0 php_value session.auto_start 0 php_value mbstring.http_input pass php_value mbstring.http_output pass php_value mbstring.encoding_translation 0 </IfModule> # Requires mod_expires to be enabled. <IfModule mod_expires.c> # Enable expirations. ExpiresActive On # <a href="/taxonomy/term/159" title="">cache</a> all files for 2 weeks after access (A). ExpiresDefault A1209600 # Do not <a href="/taxonomy/term/159" title="">cache</a> dynamically generated pages. ExpiresByType text/html A1 </IfModule> # Various rewrite rules. <IfModule mod_rewrite.c> RewriteEngine on # If your site can be accessed both with and without the 'www.' prefix, you # can use one of the following settings to redirect users to your preferred # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option: # # To redirect all users to access the site WITH the 'www.' prefix, # (http://example.com/... will be redirected to http://www.example.com/...) # adapt and uncomment the following: RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301] # # To redirect all users to access the site WITHOUT the 'www.' prefix, # (http://www.example.com/... will be redirected to http://example.com/...) # uncomment and adapt the following: # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] # Modify the RewriteBase if you are using <a href="/category/ph%C3%A2n-lu%E1%BB%93ng/drupal.html" title="">drupal</a> in a subdirectory or in a # VirtualDocumentRoot and the rewrite rules are not working properly. # For example if your site is at http://example.com/drupal uncomment and # modify the following line: # RewriteBase /drupal # # If your site is running in a VirtualDocumentRoot at http://example.com/, # uncomment the following line: # RewriteBase / # Rewrite old-style URLs of the <a href="/taxonomy/term/229" title="">form</a> 'node.php?id=x'. #RewriteCond %{REQUEST_FILENAME} !-f #RewriteCond %{REQUEST_FILENAME} !-d #RewriteCond %{QUERY_STRING} ^id=([^&]+)$ #RewriteRule node.php index.php?q=node/view/%1 [L] # Rewrite old-style URLs of the <a href="/taxonomy/term/229" title="">form</a> 'module.php?mod=x'. #RewriteCond %{REQUEST_FILENAME} !-f #RewriteCond %{REQUEST_FILENAME} !-d #RewriteCond %{QUERY_STRING} ^mod=([^&]+)$ #RewriteRule module.php index.php?q=%1 [L] # Rewrite current-style URLs of the <a href="/taxonomy/term/229" title="">form</a> 'index.php?q=x'. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} ^/.+$ RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] # Rewrite subdomain.example.com to example.com/index.php?q=subdomain RewriteCond %{REQUEST_URI} ^/$ RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC] RewriteRule ^.*$ index.php?q=%1 [L,QSA] </IfModule> # $Id: .htaccess,v 1.81.2.4 2008/01/22 09:01:39 drumm Exp $
Ứng dụng
Chúng ta có thể áp dụng chức năng sub-domain cho các trường hợp sau:
- Multi blogger sites - Mỗi user blog có một sub-domain.
- Site bán hàng - Mỗi site, mỗi shop có một domain name riêng biệt.
- Các tờ báo lớn, ...
- Kết hợp với module path của drupal để tạo các path tùy biến.
- Kết hợp với module pathauto để hệ thống tự sinh ra các path đẹp cho: các node, hồ sơ người dùng, ...
Tham khảo
- root drupal 5's .htaccess file
- Apache document: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
Update #1 (2008/Dec/26)
New custom rewrite rule, which removes domain hardcode.
<IfModule mod_rewrite.c> RewriteEngine on # Rewrite current-style URLs of the <a href="/taxonomy/term/229" title="">form</a> 'index.php?q=x'. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d ### # Rewrite subdomain.example.com to example.com/index.php?q=subdomain ### ## Only rewrite on root level: ### true: foo.example.com ### false: foo.example.com/bar ## is not www.example.com RewriteCond %{HTTP_HOST} !^(www\.){,1}([^\.]+\.(com|info|net|org))$ [NC] ## is something.example.com RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.(com|info|net|org)$ [NC] ## rewrite to example.com/index.php?q=something </IfModule>
Unsigned version
Comments
Generate URL
Hi Thế Hồng,
Hôm trước, Mình đang hỏi TH trên group như không nhớ password để login hỏi tiếp nên vào đây hỏi luôn TH cho tiện.
Vấn đề từ clean URL và dùng .htaccess để chuyển về url của drupal dạng ?q=xyz thì không vấn đề gì cả.
Vấn đề hôm trước mình thắc mắc là hệ thống url của drupal, kể cả pathauto mod của drupal không cho phép sinh ra url dạng http://sub.domain.com (ví dụ lấy term làm sub như http://term1.domain.com). Một số domain quản lý được dạng này đều phải patch vào mod path. TH có giải pháp nào làm chuyện này mà không cần patch path mod không?
Thks
Cái này thì phải hack
Cái này thì phải hack rồi, nếu sử dụng module views, bạn có thể sử dụng một số hook của views để xử lý ra được mảng $subdomains, và hack hàm url ra dạng sau (drupal 5):
<?php
function url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE) {
if (isset($fragment)) {
$fragment = '#'. $fragment;
}
// Return an external link if $path contains an allowed absolute URL.
// Only call the slow filter_xss_bad_protocol if $path contains a ':' before any / ? or #.
$colonpos = strpos($path, ':');
if ($colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && filter_xss_bad_protocol($path, FALSE) == check_plain($path)) {
// Split off the fragment
if (strpos($path, '#') !== FALSE) {
list($path, $old_fragment) = explode('#', $path, 2);
if (isset($old_fragment) && !isset($fragment)) {
$fragment = '#'. $old_fragment;
}
}
// Append the query
if (isset($query)) {
$path .= (strpos($path, '?') !== FALSE ? '&' : '?') . $query;
}
// Reassemble
return $path . $fragment;
}
global $base_url, $subdomains;
static $script;
static $clean_url;
if (!isset($script)) {
// On some web servers, such as IIS, we can't omit "index.php". So, we
// generate "index.php?q=foo" instead of "?q=foo" on anything that is not
// Apache.
$script = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === FALSE) ? 'index.php' : '';
}
// Cache the clean_url variable to improve performance.
if (!isset($clean_url)) {
$clean_url = (bool)variable_get('clean_url', '0');
}
$base = ($absolute ? $base_url . '/' : base_path());
// The special path '<front>' links to the default front page.
if (!empty($path) && $path != '<front>') {
$_path = $path;
$path = drupal_get_path_alias($path);
$path = drupal_urlencode($path);
if ($subdomains[$_path]) {
$base = preg_replace ('|http://w{0,3}|', 'http://' . $subdomains[$_path] . '.', $base_url);
if ($subdomains[$_path] == $path) {
return $base;
}
}
if (!$clean_url) {
if (isset($query)) {
return $base . $script .'?q='. $path .'&'. $query . $fragment;
}
else {
return $base . $script .'?q='. $path . $fragment;
}
}
else {
if (isset($query)) {
return $base . $path .'?'. $query . $fragment;
}
else {
return $base . $path . $fragment;
}
}
}
else {
if (isset($query)) {
return $base . $script .'?'. $query . $fragment;
}
else {
return $base . $fragment;
}
}
}
?>
Nói thêm về cách xây dựng biến toàn cục $subdomains
Giả sử các node được trình bày ra giao diện thông qua views.
1. Sử dụng hook_views_table để định nghĩa một views-field
<?php/**
* Implementation of hook_views_tables
*/
function something_views_tables () {
return array (
'subdomain' => array(
'name' => 'node',
'fields' => array(
'alias' => array(
'name' => t('Subdomain: Group alias'),
'handler' => 'something_handler_field_alias',
'notafield' => TRUE
)
)
)
);
}
?>
2. Một hàm để handle views-field đã định nghĩa
<?php
/**
* Tìm alias của shop và lưu vào mảng $subdomains
*/
function something_handler_field_alias ($fieldinfo, $fielddata, $value, $data) {
global $subdomains;
if ($data && !isset ($subdomains['node/' . $data->nid])) {
// ...
// Do your job here //
// ...
if (!empty ($alias)) {
$subdomains['node/' . $data->nid] = $alias;
}
}
}
?>
3. Sử dụng hook_views_pre_query để tự động chèn views-field đã định nghĩa vào tất cả các view
<?phpfunction dv_og_views_pre_query (&$view) {
array_unshift (
$view->field,
array (
'vid' => 16,
'tablename' => 'subdomain',
'field' => 'alias',
'handler' => '',
'sortable' => 0,
'defaultsort' => '',
'options' => '',
'position' => 0,
'fullname' => 'subdomain.alias',
'id' => 'subdomain.alias',
'queryname' => 'subdomain_alias'
)
);
}
?>
Post new comment