【发布时间】:2021-06-10 00:16:14
【问题描述】:
我有一个名为 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>
但它不起作用。当我访问 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
【问题讨论】:
-
并且大概其他页面的 URL 看起来像
/project/<page-url>? CI 本身是否已经针对这种 URL 格式进行了配置?您将哪些 URL 用于静态资源?这些是否也省略了/public目录? -
@MrWhite 是的,其他页面的 url 看起来就像你说的那样。是的,CI 可以像这样使用这种用途,因为我有另一个带有 CI4 的网站,它从所有 url 中省略了 public/index.php,不同之处在于另一个正在运行的网站不在子域上,而是在根目录上,所以不是同一个htaccess
标签: .htaccess codeigniter codeigniter-4