2011-02-26 87 views
0

我挣扎写.htaccess文件基本上映射以下领域:Apache的mod_rewrite的

  • dev.domain.com/$1 => index.php/dev/$1
  • api.domain.com/$1 => index.php/api/$1
  • domain.com/$1 => index.php/front/$1
  • www.domain.com/$1 => index.php/front/$1

目前,一切都映射到index.php/$1与以下confi guration:

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-f 

    RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule> 

这就是我使用默认情况下使用的PHP框架。任何帮助将不胜感激:-)谢谢!

回答

2

如果您想根据域名映射请求的路径,那么您可以使用额外的RewriteCond并首先匹配HTTP_HOST

你将不得不乘以现有RewriteConds块和每个域的重写规则 - 除了最后一对可能仅仅是一个默认的:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{HTTP_HOST} ^dev\.domain\.com 
RewriteRule ^(.*)$ index.php/dev/$1 [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{HTTP_HOST} ^api\.domain\.com 
RewriteRule ^(.*)$ index.php/api/$1 [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php/front/$1 [L] 

未经测试。但请参阅serverfault Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?上的文章,它解释了条件和重写规则之间的交互。

+0

谢谢!我如何让'域\ .com'成为任何东西?如果这可以在不同领域的生产环境和开发环境中运行,那就太好了:) – tarnfeld 2011-02-26 13:10:44

+0

@tarnfeld:要允许所有的东西都可以忽略一个规则块的条件。像最后一个例子一样。 (或者与'。*'匹配对称)。 – mario 2011-02-26 13:16:52

+0

我结束了使用'^ dev \。*'这似乎工作正常:) – tarnfeld 2011-02-26 13:19:06