动态子域+通过htaccess定义的子域

时间:2010-08-16 19:48:31

标签: .htaccess mod-rewrite subdomain wildcard-subdomain

我讨厌询问有关mod_rewrite的问题,但我似乎无法让这些规则正常运行。我曾经设置好并且曾经想过一次,但是几年过去了,现在它只是没有玩得很好而且我的mod_rewrite知识又回到了呃......

基本上我想做的是在主域example.com上强制使用www。 (或者强迫没有www会更好,但我没有远程能够与动态子域一起正常工作)。

如果存在my.example.com规则,请相应地遵循它们

对于其他任何内容,即:* .example.com,传递给另一个文件进行解析。

我目前处理强制www的规则,并正确处理动态子域,但完全忽略了my.example.com规则,将其作为通配符子域处理。似乎我可以让我的任何一条规则正常工作,但是当试图让它们一起工作时,它只是一个混蛋。如果有人知道必须让这个工作正常,那将非常感激。

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# force www on main, force no www would be better.
RewriteCond %{HTTP_HOST} !^(.*)\.example.com$ [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# my.example.com  defined subdomain
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^.*$ - [S=3]
RewriteRule ^$ /mydirectory/index.php [L]
RewriteRule ^category/([^/]+)/?$ /mydirectory/index.php?category=$1  [L]
RewriteRule ^submit/?$ /mydirectory/process.php [L]

# *.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.examples$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]

1 个答案:

答案 0 :(得分:2)

请尝试以下规则:

# remove www on main
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

# my.example.com  defined subdomain
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^.*$ - [S=3]
RewriteRule ^$ /mydirectory/index.php [L]
RewriteRule ^category/([^/]+)/?$ /mydirectory/index.php?category=$1  [L]
RewriteRule ^submit/?$ /mydirectory/process.php [L]

# *.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]

现在,第一条规则会将+ www.example.com *重定向到 example.com 。第三条规则将捕获对非现有主机的任何请求。

相关问题