Fil*_*ilT 3 .htaccess codeigniter codeigniter-4
我有一个名为的子域test.mysite.com,并且在名为 project 的文件夹内安装了 CI4。所以 CI4 安装的实际 url 是test.mysite.com/project/public/index.php。我想从 URL 中删除 public/index.php 部分,但继续使用 public 文件夹来保存我的文件,因为它们应该这样做。
我在项目文件夹根目录上使用这个 .htaccess:
DirectoryIndex /public/index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./public/index.php/$1 [L]
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
Run Code Online (Sandbox Code Playgroud)
但这不起作用。当我访问 test.mysite.com/project 时,它会将我带到服务器文件列表。我知道 .htaccess 文件正在被正确读取,因为当我在那里添加错误时,它会给我一个警告
编辑: CI4 已经在公共文件夹中附带了一个 htaccess:
# Disable directory browsing
Options All -Indexes
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
# RewriteBase /
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 index.php
</IfModule>
# Disable server signature start
ServerSignature Off
# Disable server signature end
Run Code Online (Sandbox Code Playgroud)
\n\n当我访问 test.mysite.com/project 时,它会引导我进入服务器文件列表
\n
因为您的DirectoryIndex设置为/public/index.php(可能不存在,因为索引文档位于/project/public/index.php)并且目录索引(mod_autoindex)可能已启用(应该禁用它,因此此类请求会导致 403 Forbidden)。
\n\n不同之处在于,正在运行的其他网站不在子域上,而它\xe2\x80\x99s位于根目录上,因此它\xe2\x80\x99s与htaccess不同
\n
我不确定为什么会有所不同?
\n使用子目录.htaccess中的文件/project,按如下方式排列 mod_rewrite (和 mod_dir)指令:
DirectoryIndex index.php\nOptions -Indexes\n\nRewriteEngine on\nRewriteCond $1 !^public/(index\\.php|images|assets|css|js|robots\\.txt|favicon\\.ico)\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule (.*) public/index.php/$1 [L]\nRun Code Online (Sandbox Code Playgroud)\nrobots\\.txt第一个条件中和 的存在favicon\\.ico意味着您正在重写来自文档根的请求。由于搜索引擎请求/robots.txt(在文档根目录中),而不是/project/robots.txt(或/project/public/robots.txt)。这同样适用于/favicon.ico. 如果您不重写这两个请求,则不需要这两个条目。
这还假设您使用子目录直接链接到静态资源public。例如。/projects/public/images/foo.jpg。这不一定是可取的,因为它会/public在 URL 路径中公开。用户不一定会看到它,因为它在浏览器的地址栏中不直接可见,但搜索引擎和任何查看 HTML 源/网络流量的人都会看到它。
只是补充一下,第一个条件(即RewriteCond指令)“只是”一个优化。如果设置不正确,您的站点可能会正常工作,并且您不会看到任何差异,只是它将执行比需要执行的更多的文件系统检查。
另一种方法是拥有两个.htaccess文件。一个基本/project/.htaccess文件只是将所有内容重写到public子目录中,而一个更全面的 (CI).htaccess文件/project/public/.htaccess实际上将请求路由到 CI。这样您就可以省略public所有URL,包括静态资源的 URL。
例如:
\n/project/.htaccessDirectoryIndex index.php\nOptions -Indexes\n\nRewriteEngine On\n\n# Unconditionally rewrite everything to the "public" subdirectory\nRewriteRule (.*) public/$1 [L]\nRun Code Online (Sandbox Code Playgroud)\n/project/public/.htaccess子目录文件中 mod_rewrite 指令的存在自然可以防止父目录中指令.htaccess的重写循环。RewriteRule(假设 mod_rewrite 继承尚未启用。)
RewriteEngine on\n\nRewriteCond $1 !^(index\\.php|images|assets|css|js|robots\\.txt|favicon\\.ico)\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule (.*) index.php/$1 [L]\n\n<IfModule mod_headers.c>\n <FilesMatch "\\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">\n Header set Access-Control-Allow-Origin "*"\n </FilesMatch>\n</IfModule>\nRun Code Online (Sandbox Code Playgroud)\n