Apache Mod_Rewrite

发布于 2024-10-19 11:37:00 字数 622 浏览 0 评论 0原文

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

I'm struggling writing a .htaccess file to basically map the following domains:

  • 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

At the moment, everything is mapping to index.php/$1 with the following configuration:

<IfModule mod_rewrite.c>
    RewriteEngine on

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

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

This is what came with the php framework I'm using by default. Any help would be greatly appreciated :-) Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香橙ぽ 2024-10-26 11:37:00

如果你想根据域名映射请求的路径,那么你可以使用额外的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 的文章 关于 Mod_Rewrite 规则您想知道但又不敢问的一切?,它解释了条件和重写规则之间的相互作用。

If you want to map the requested paths based on the domain name, then you can use an additional RewriteCond and match the HTTP_HOST first.

You will have to multiply your block of existing RewriteConds and the RewriteRule for each domain - except for the last pair which could just be a default:

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]

Not tested. But see also the article on serverfault Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?, which explains the interactions between conditions and rewrite rules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文