我有两个 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/