htaccess的子域htaccess

2023-09-02 00:19:31 作者:见到棺材也不掉泪

HOWTO使这与htacess:

  subdomain.domain.com  - > domain.com/subdomain(在客户端没有重定向)
domain.com/subdomain  - > subdomain.domain.com(重定向)
 

我把冷杉重定向:

 重写规则^子域/  -  [L]
的RewriteCond%{HTTP_HOST} ^ subdomain.domain.com [NC]
重写规则(。*)子域/ $ 1 [L]
 

它是如何在形式htacess插入的:

  AddDefaultCharset UTF-8
DirectoryIndex的index.php文件的index.html index.htm的
选项​​所有-Indexes
ErrorDocument的404 /404.html
ErrorDocument的403 /404.html
RewriteEngine叙述上
的RewriteCond%{HTTP_HOST} ^ domain.com
重写规则(。*)http://www.domain.com/$1 [R = 301,L]
的RewriteCond%{REQUEST_URI} !(PNG | JPG | JPEG | GIF)$ [NC]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则(。*)$的index.php / $ 1 [QSA]
 

解决方案

这是一个基于通配符的解决方案,所以它的工作前人的精力用于任何数量的子域。

这将重定向 domain.com/foo/bar foo.domain.com/bar

 的RewriteCond%{HTTP_HOST} ^ domain.com $ [NC]
重写规则([^ /] +)(/.* |)$ 1.domain.com / $ 2 [R = 302]
 

这将处理(内部重写)虚拟主机:

 的RewriteCond%{HTTP_HOST} ^(。*)。domain.com $ [NC]
重写规则(。*)%1 / $ 1 [L]
 

您可以考虑使用,而不是302 301(永久性重定向)。

我强烈建议你有一个虚拟主机为您的主要网站的 domain.com 或 www.domain.com 的和一个单独的一个用于处理所述虚拟子域

有关只有某个子域:

 的RewriteCond%{HTTP_HOST} ^ domain.com $ [NC]
重写规则(子域)(/.* |)$ 1.domain.com / $ 2 [R = 302,L]

的RewriteCond%{HTTP_HOST} ^(子域).domain.com $ [NC]
重写规则(。*)%1 / $ 1 [L]
 

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]

解决方案

This is a wildcard based solution, so it sould work for any number of subdomains.

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]

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:

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]