【问题标题】:Remove CI4 public/index.php with .htaccess rewrite rule on subdomain/folder在子域/文件夹上使用 .htaccess 重写规则删除 CI4 public/index.php
【发布时间】: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/&lt;page-url&gt;? CI 本身是否已经针对这种 URL 格式进行了配置?您将哪些 URL 用于静态资源?这些是否也省略了/public 目录?
  • @MrWhite 是的,其他页面的 url 看起来就像你说的那样。是的,CI 可以像这样使用这种用途,因为我有另一个带有 CI4 的网站,它从所有 url 中省略了 public/index.php,不同之处在于另一个正在运行的网站不在子域上,而是在根目录上,所以不是同一个htaccess

标签: .htaccess codeigniter codeigniter-4


【解决方案1】:

当我访问 test.mysite.com/project 时,它会引导我进入服务器文件列表

因为您的DirectoryIndex 设置为/public/index.php(它可能不存在,因为索引文档位于/project/public/index.php)并且目录索引(mod_autoindex)可能已启用(它应该被禁用,以便这样请求会导致 403 Forbidden)。

不同的是,另一个正在运行的网站不在子域上,而是在根目录下,所以它不是同一个 htaccess

我不知道为什么会有所不同?

使用 /project 子目录中的 .htaccess 文件,将您的 mod_rewrite(和 mod_dir)指令安排成这样:

DirectoryIndex index.php
Options -Indexes

RewriteEngine on
RewriteCond $1 !^public/(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) public/index.php/$1 [L]

robots\.txtfavicon\.ico 在第一个条件中的存在意味着您正在重写来自文档根目录的请求。由于搜索引擎请求/robots.txt(在文档根目录中),而不是/project/robots.txt(或/project/public/robots.txt)。这同样适用于/favicon.ico。如果您不重写这两个请求,则不需要这两个条目。

这还假设您使用public 子目录直接链接到您的静态资源。例如。 /projects/public/images/foo.jpg。这不一定是可取的,因为它在 URL 路径中公开了/public。用户不一定会看到它,因为它在浏览器的地址栏中并不直接可见,但搜索引擎和任何查看 HTML 源/网络流量的人都会看到它。

补充一下,第一个条件(即RewriteCond 指令)“只是”一个优化。如果设置不正确,您的站点可能会正常运行,并且您不会看到任何差异,只是它将执行比它需要执行的更多的文件系统检查。

替代结构

另一种方法是拥有两个.htaccess 文件。一个基本的/project/.htaccess 文件,它只是将一切 重写到public 子目录和一个更全面的(CI).htaccess 文件/project/public/.htaccess,它实际上将请求路由到 CI。然后,这允许您从 所有 URL 中省略 public,包括静态资源的 URL。

例如:

/project/.htaccess

DirectoryIndex index.php
Options -Indexes

RewriteEngine On

# Unconditionally rewrite everything to the "public" subdirectory
RewriteRule (.*) public/$1 [L]

/project/public/.htaccess

子目录.htaccess 文件中存在mod_rewrite 指令自然会阻止父目录中RewriteRule 指令的重写循环。 (假设未启用 mod_rewrite 继承。)

RewriteEngine on

RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) 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>

【讨论】:

  • 非常感谢您的解释。我是修改 htacess 文件的新手。我已经在 CI 的project/public 中有一个带有一些代码的 htaccess 文件。如果您愿意,我可以编辑我的答案以显示它。只是添加第一段代码似乎不起作用,因为它给了我一个 403 Forbidden。替代版本给了我一个 404
  • public htacces DirectoryIndex 更改为 /project/public/index.php 就像你说的那样修复它,同时还更改 CI 文件中的路由以获取“项目”上的默认控制器。另一种方法是我无法上班,除了主页之外,我无法访问网站上的任何其他内容。试图看看它可能是什么
猜你喜欢
  • 2017-07-22
  • 2012-11-22
  • 1970-01-01
  • 2014-12-20
  • 2015-03-21
  • 2012-08-20
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多