【问题标题】:Apache Mod_RewriteApache Mod_Rewrite
【发布时间】:2011-02-26 21:00:18
【问题描述】:

我正在努力编写一个 .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 框架附带的。任何帮助将不胜感激:-) 谢谢!

【问题讨论】:

    标签: php .htaccess mod-rewrite apache2


    【解决方案1】:

    如果您想根据域名映射请求的路径,则可以使用额外的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? 的文章,其中解释了条件和重写规则之间的交互。

    【讨论】:

    • 谢谢!我如何允许domain\.com 成为任何东西?如果它可以在不同域的生产和开发环境中运行,那就太好了:)
    • @tarnfeld:要允许所有内容,您可以省略一个规则块的条件。就像上一个例子一样。 (或者为了对称匹配.*。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2015-07-04
    • 2013-07-12
    • 1970-01-01
    相关资源
    最近更新 更多