【问题标题】:Dynamic subdomains + defined subdomains via htaccess动态子域 + 通过 htaccess 定义的子域
【发布时间】:2011-03-30 15:11:24
【问题描述】:

我讨厌询问有关 mod_rewrite 的问题,但我似乎无法让这些规则正常工作。我之前已经设置并弄清楚了一次,但几年过去了,现在它只是玩得不好,我的mod_rewrite知识又回到了uhhhh......

基本上我想做的是在主域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]

【问题讨论】:

  • 嗯...似乎在我的测试服务器上工作正常。 my实际上是my,还是别的什么?

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


【解决方案1】:

试试这些规则:

# 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。第三条规则将捕获对不存在的主机的任何请求。

【讨论】:

  • 这似乎工作得很好。在蒂姆的评论之后,我再次清除了我的缓存并且我的原始规则开始工作,所以像往常一样,这主要是我的用户错误,但我很高兴没有 www 工作,非常感谢 Gumbo!
猜你喜欢
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
  • 2014-03-29
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多