Subdomains with .htaccess

Posted: สิงหาคม 27, 2009 in คอมพิวเตอร์และอินเทอร์เน็ต
เขียน rewrite rule เพื่อแปลง subdomain จาก ให้เรียก
โดยที่ชื่อโดเมนยังคงเป็น เหมือนเดิม

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^backend\.example
RewriteRule (.*) /backend/$1 [L]


Another way to do this is to rewrite <subdomain> to /sd_<subdomain>/ — That is, write each subdomain to a uniquely-named subdirectory. You could also name the subdirectories "user-<subdirectory>", or the familiar "~<subdomain>", or anything like that, but the prefix needs to be something that you ‘reserve’ for use only in naming first-level subdomain subdirectories.

In those cases, you could use:

# Rewrite subdomain requests to subdirectories except for
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /sd_%1/$1 [L]
# Redirect direct user-agent requests for<subdomain>/<page&gt; to http://<subdomain&gt;<page>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sd_(.+)\ HTTP/
RewriteRule ^sd_([^/]+)/(.*)$ http://$$2 [R=301,L]

The first section supports all subdomain requests, while the second section keeps users and search engines from finding the ‘real’ location of the files under<subdomain>, thus preventing curiosity-seekers from finding them and duplicate-content problemns in search engine results.




Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s