Mod_rewrite .htaccess子域,但区分干净的子域和pathed URL

时间:2013-11-19 03:12:20

标签: apache .htaccess mod-rewrite wildcard subdomain

我如何mod_rewrite(在.htaccess中)执行此操作?

{subdomain}.thedomain.com

OR

{subdomain}.thedomain.com/

......应改写为:

thedomain.com/index.php?do={subdomain}

....但是,所有其他文件或目录应该在没有子域部分的情况下重写,其余部分保持原样。         例如

xyz.thedomain.com/something/somethingelse/somefile.jpg 

----------->

thedomain.com/something/somethingelse/somefile.jpg

1 个答案:

答案 0 :(得分:0)

只要您的子域名指向与主域相同的文档根目录,您只需将这些规则添加到文档根目录中的htaccess文件中。

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.thedomain\.com$ [NC]
RewriteRule ^(.+)$ http://thedomain.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.thedomain\.com$ [NC]
RewriteRule ^/?$ /index.php?do=%1 [L]