codeigniter - 删除 index.php 并处理两个 Codeigniter 站点的子域(当其中一个站点位于另一个站点时)

标签 codeigniter .htaccess mod-rewrite

我有两个 Codeigniter 站点,一个位于另一个站点的子目录中。我需要一些帮助来修改我的 htaccess 文件以从两者中删除 index.php。

第一个站点 http://subdomain.domain.com 存储在 /home/sites/subdomain/www/html/

第二个,http://test.subdomain.domain.com位于/home/sites/subdomain/www/html/app_staging/

这是我的 .htacess 文件,来自 /home/sites/subdomain/www/html/:

RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !test.subdomain.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

# Rewrite the sub domain
RewriteCond %{HTTP_HOST} test.subdomain.domain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /app_staging/index.php?/$1 [L]

这将从第一个站点中删除index.php,那里一切正常。在第二个站点上,我可以加载默认 Controller 表单 http://test.subdomain.domain.com,但后续页面会从服务器返回 404。我的下一步是什么?

最佳答案

您应该将两个 .htaccess 文件分别放入其自己的根 CI 文件夹中。

就个人而言,我更喜欢限制请求的文件和/或文件夹,以防止它们被 RewriteRule 处理。

因此,对于位于 /home/sites/subdomain/www/html/ 的第一个文件:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico|public)
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

另一个位于 /home/sites/subdomain/www/html/app_staging/:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico|public)
    RewriteRule ^(.*)$ /app_staging/index.php/$1 [L]
</IfModule>

注意:您必须将 css 和其他文件夹(如 public) 添加到 RewriteCond 如果您不希望 RewriteRule 处理它们。

旁注:您可能希望通过在每个 .htaccess 文件中使用以下内容来防止目录列出:

<IfModule mod_autoindex.c>
    Options -Indexes
</IfModule>

关于codeigniter - 删除 index.php 并处理两个 Codeigniter 站点的子域(当其中一个站点位于另一个站点时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18071203/

相关文章:

php - 第三方资源在 CodeIgniter 中的位置在哪里?

wordpress - 将 wordpress 站点转换为 HTTPS

php - 如何在 PHP 中使用 HTTP 身份验证,然后针对数据库运行输入的数据?

php - Apache 重写规则和 POST 数据

php - 比较两个数组,如果数组匹配则返回 id

javascript - 使用引导模式在数据表库中插入数据

php - Codeigniter 数据库无法仅通过一台服务器上的框架进行连接

.htaccess - 如何在 htaccess 中启用阿拉伯语 slug?

php url重写将页面从旧折叠移动到新折叠

regex - Htaccess 重定向 https 和 http 以及自定义 url