**** Subdomains using Mod_rewrite and Wildcard DNS *

เริ่มโดย ggynmsn, 04 สิงหาคม 2007, 01:45:41

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

ggynmsn

 :)

Sub-Subdomains using Mod_rewrite and Wildcard DNS

In an internet-savvy world, subdomains can play a big role in user awareness. Descriptive subdomains are easy to remember, strengthen the credibility of a website and encourage a visitor to bookmark it.


Quick example:
a) http://tomcat.apache.org
b) http://www.jguru.com/faq/home.jsp?topic=Tomcat
Which one would you prefer?

Even better, subdomains and sub-subdomains can be pointed to dynamic content using Apache's mod_rewrite and Wildcard DNS entries.
Called: http://topic.domain.com
Served: http://www.domain.com/generator.php?topic (invisible)
The visitor requests http://topic.domain.com while the actual content is dynamically served by a PHP script in the background.

Requirements
To realize this on your server,
- a DNS server with support for wildcard DNS entries (eg. BIND) has to be available,
- this DNS server must be the authorative DNS server for your domain,
- Apache with mod_rewrite has to be installed.

Authorative Nameserver for your domain
This means that you need to setup a NS entry and point it to your Nameserver. The corresponding line for your zone file is

domain.com NS ns.domain.com
Add a Wildcard DNS record to your Nameserver
Wildcard DNS records match all requests for domain names without a record. You can set up a wildcard record to match all subdomains of your domain like this:

*.domain.com. A 1.2.3.4
where 1.2.3.4 is the public IP address of your webserver. Be sure to include the last dot in your FQDN.

Configure Apache Wildcard support
Assuming your Apache config uses vHosts, your new configuration would look like this:


<virtualhost>
ServerName domain.com:80
ServerAlias *.domain.com
...
..
</virtualhost>
The ServerAlias statement enables Wildcard support for your domain. From now on, your Apache vHost will react on non-existent subdomains.

Configure redirects using mod_rewrite
To activate proper subdomain redirects, save the text below, put it into a .htaccess file and upload it into the the http root directory of your domain.


RewriteEngine On
#prevents rewrite loops
RewriteCond %{REQUEST_URI} !^/generator.php?(.*)?$
# match the subdomain
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com$ [NC]
# append the path
RewriteRule ^(.*)$ /generator.php?%2/$1 [QSA,L]
Test your redirect
This PHP code captures the name of your subdomain. You can save it and upload it as generator.php to test your subdomain setup.
<?php $server_host = explode('.', $_SERVER['HTTP_HOST']); $subdomain = strtolower($server_host[0]); echo 'Subdomain name is: ' . $subdomain; ?>
Visit an imaginary subdomain, eg. http://test223.domain.com.
If everything works, you'll see "test223" on your screen which is dynamically generated by generator.php.

Same setup for Sub-Subdomains
If you plan to use sub-subdomains instead, you can apply this guide with a couple of changes to an existing subdomain:
- Setup an authorative NS for your Subdomain (sub.domain.com NS ns.domain.com)
- Add a wildcard DNS record for your Subdomain (*.sub.domain.com. A 1.2.3.4)
- Add a Wildcard alias in the Apache vHost of your existing subdomain (ServerAlias *.sub.domain.com)
- Change the mod_rewrite code accordingly

น้องบิ้ว


catz001

 :) มีประโยชน์มากเลยครับ ขอบคุณครับ

harmonic



Nemesis

[direct=http://kiwifairy.com]ชุดราตรีคนอ้วน สาวอวบ พร้อมส่ง[/direct]

guy852


roteee

#7
ขอบคุณสำหรับความรู้ครับ ...

ขอเพิ่มเติมเล็กน้อย จากประสบการณ์ตรง:
- sub.domain.com เท่ห์ แต่ปัญหาจะตามมาสารพัด เช่น
   > ข้าม sub แล้ว session ไม่ไป ต้องจัดการกันสารพัดวิธี เปลืองพลังของทั้งโปรแกรมเมอร์ และพลังเซิฟเวอร์อีก
   > หากทำเกี่ยวกับ web on mobile จะมีปัญหากับ mobile operator มาก เช่น wap gateway จะ default เพื่อรับรู้ว่านี่คือ mobile version น้ะ ด้วย m.xxx.com และ wap.xxx.com (อันอื่นมีป่าวไม่รู้น้ะ แต่ที่รู้จักอยู่ 2 อัน) และหากพี่ wap gateway เห็น sub อันอื่นๆ พี่แกก็จะทำงานประหนึ่งว่ากำลังเปิด web ไม่ใช่ wap อย่างของบางเจ้า เขาก็มีโปรแกรม mobile internet มาเอาหน้า web/wap ของเราไปตัด แต่ง (หวังดีมากเกินไป) แล้วแป่ะ banner ตัวเท่าควายไว้บนหัวซะงั้น ...
    > การร้องขอเพื่อให้พี่ mobile operator config บางสิ่งบางอย่างบน wap gateway ให้ จะทำได้ในลักษณะ domain.com/* ทำ *.domain.com/ มิได้
   > สมัยใหม่ๆ มารู้สึก domain.com/sub ก็เป็นที่นิยมน้ะครับ ... twitter, facebook อิอิ
- domain.com/sub ไม่เท่ห์เท่าไหร่ แต่มันดีสำหรับผมมากกว่าแบบแรก  :wanwan003: :wanwan003: :wanwan003:

ไม่ได้ว่าอันไหนดีไม่ดีน้ะครับ แค่มาแชร์ประสบการณ์ :D

itmoamun

อ้างถึงจาก: Nemesis ใน 06 สิงหาคม 2007, 01:00:44
แล้วถ้าจาก domain.com/sub ---> sub.domain.com ทำไงอ่าครับ  :-*

ก็บทความนี้ก็บอกอยู่แล้วนะครับ ทำตามที่ จขกท. บอกก็ได้แล้ว
[direct=https://www.itmoamun.com/it-news/]ข่าวไอที[/direct],[direct=https://www.homethaimarket.com]ตลาดบ้าน[/direct], [direct=https://shoppingplearn.com]ช้อปปิ้ง[/direct],[direct=https://www.safetyppe.com]อุปกรณ์เซฟตี้[/direct], [direct=https://www.kasetintree.com]เกษตรอินทรีย์[/direct]

thai2ads

อ้างถึงข้าม sub แล้ว session ไม่ไป
ถ้าจะข้าม subdomain ใช้คำสั่งนี้ก็น่าจะได้นะครับ
php_value session.cookie_domain ".domain.com"
รับทำเว็บไซต์ เขียนระบบต่างๆตามที่ต้องการ
รับทำเว็บไซต์ | ทองคำราคาวันนี้