Subdomains with .htaccess

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

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

Ref#
http://www.webmasterworld.com/forum92/3555.htm

Another way to do this is to rewrite <subdomain>.example.com 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 http://www.example.com
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 http://www.example.com/sd_<subdomain>/<page&gt; to http://<subdomain&gt;.example.com/<page>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sd_(.+)\ HTTP/
RewriteRule ^sd_([^/]+)/(.*)$ http://$1.example.com/$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 http://www.example.com/sd_<subdomain>, thus preventing curiosity-seekers from finding them and duplicate-content problemns in search engine results.

Jim



Advertisements

ใส่ความเห็น

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

WordPress.com Logo

You are commenting using your WordPress.com 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