htaccess子域
如何使用 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 表单中:
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]
Howto make this with htacess:
subdomain.domain.com -> domain.com/subdomain (no redirect on client side)
domain.com/subdomain -> subdomain.domain.com (redirect)
I make firs redirect:
RewriteRule ^subdomain/ - [L]
RewriteCond %{HTTP_HOST} ^subdomain.domain.com [NC]
RewriteRule (.*) subdomain/$1 [L]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个基于通配符的解决方案,因此它适用于任意数量的子域。
这会将
domain.com/foo/bar
重定向到foo.domain.com/bar
:这将处理(内部重写)虚拟主机:
您可能会考虑使用 301 (永久重定向)而不是 302。
我强烈建议您为您的主站点 domain.com 或 www.domain.com 设置一个
VirtualHost
,并且用于处理虚拟子域的单独的域。仅针对某个子域:
This is a wildcard based solution, so it sould work for any number of subdomains.
This will redirect
domain.com/foo/bar
tofoo.domain.com/bar
:This will handle (internal rewrite) the virtual hosts:
You might consider using 301 (permanent redirect) instead of the 302.
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.For only a certain subdomain: