Giới thiệu Drupal và sub-domain

Giới thiệu 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/thehongthì đẹp hơn toila.net/index.php?q=user/1, web.com/thongbaosẽ dễ nhớ hơn web.com/index.php?q=taxonomy/term/1+2+3rất nhiều hay là thehong.yeublog.comsẽ 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

  1. Apache/server hỗ trợ mod_rewrite, cấu hình trên từng thư mục bằng tập tin .htaccess.
  2. Có cài đặt module rewrite.
  3. 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ạng http://foo.domainname.com/node/1, server sẽ gọi Drupal site của bạn với URL có dạng http://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.comvà 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:

  1. http://foo.domainname.com/ => http://domainname.com/index.php?q=foo
  2. 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 đó, .htaccesscủ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, ...
Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Cài đặt LESS CSS Apache module

Cài đặt LESS CSS Apache module

This is an Apache2 module that is written to compile LESS code inside the CSS files when they are requested so the process will be transparent from any script using CSS no matter what it’s language is.

Cầu thủ Oscar - 4 năm anh đút túi 100 triệu đô sau thuế

Cầu thủ Oscar - 4 năm anh đút túi 100 triệu đô sau thuế

Từ chỗ được đánh giá cao hơn Kevin De Bruyne, Oscar bây giờ không được CLB châu Âu nào ngó ngàng và mất vị trí trên tuyển Brazil suốt 5 năm qua

CNN điều tra nhà máy Foxconn - Phỏng vấn công nhân lắp ráp iPad tại Trung Quốc

CNN điều tra nhà máy Foxconn - Phỏng vấn công nhân lắp ráp iPad tại Trung Quốc

Mặc cho Apple và Foxconn luôn khẳng định bảo vệ người lao động, hiện thực có vẻ hoàn toàn trái ngược.

Công ty diệt chuột T&C

 

Diet con trung