我正在努力编写一个.htaccess文件来基本映射以下域名:
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
:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
这是我默认使用的php框架。任何帮助将不胜感激:-)谢谢!
答案 0 :(得分:2)
如果您想根据域名映射请求的路径,那么您可以使用额外的RewriteCond
并首先匹配HTTP_HOST
。
您必须将每个域的现有RewriteConds和RewriteRule块相乘 - 除了最后一对可能只是默认值:
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?的文章,该文章解释了条件和重写规则之间的相互作用。