htaccess 子域
如何使用 htacess:
Howto make this with htacess:
subdomain.domain.com -> domain.com/subdomain (no redirect on client side)
domain.com/subdomain -> subdomain.domain.com (redirect)
我首先重定向:
RewriteRule ^subdomain/ - [L]
RewriteCond %{HTTP_HOST} ^subdomain.domain.com [NC]
RewriteRule (.*) subdomain/$1 [L]
htacess的形式是怎么插入的:
How is it inserted in the form htacess:
AddDefaultCharset utf-8
DirectoryIndex index.php index.html index.htm
Options All -Indexes
ErrorDocument 404 /404.html
ErrorDocument 403 /404.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !.(png|jpg|jpeg|gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ index.php/$1 [QSA]
这是一个基于通配符的解决方案,因此它适用于任意数量的子域.
This is a wildcard based solution, so it sould work for any number of subdomains.
这会将 domain.com/foo/bar
重定向到 foo.domain.com/bar
:
This will redirect domain.com/foo/bar
to foo.domain.com/bar
:
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ([^/]+)(/.*|$) $1.domain.com/$2 [R=302]
这将处理(内部重写)虚拟主机:
This will handle (internal rewrite) the virtual hosts:
RewriteCond %{HTTP_HOST} ^(.*).domain.com$ [NC]
RewriteRule (.*) %1/$1 [L]
您可以考虑使用 301(永久重定向)而不是 302.
You might consider using 301 (permanent redirect) instead of the 302.
我强烈建议您为您的主站点 domain.com 或 www.domain.com 设置一个 VirtualHost
和一个单独的用于处理虚拟子域.
I strongly suggest you have a VirtualHost
for your main site domain.com or www.domain.com and a separate one for handling the virtual subdomains.
仅针对某个子域:
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule (subdomain)(/.*|$) $1.domain.com/$2 [R=302,L]
RewriteCond %{HTTP_HOST} ^(subdomain).domain.com$ [NC]
RewriteRule (.*) %1/$1 [L]