我希望URL像www.sample.com/controller这样正常,而不是www.sample.com/public/index.php/controller。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
我的 .htaccess 文件
我希望URL像www.sample.com/controller这样正常,而不是www.sample.com/public/index.php/controller。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
我的 .htaccess 文件
只需做以下步骤:
第一步:在 public/ 目录下,将 index.php 和 .htaccess 复制到您的项目根目录。
第二步:在项目根目录中,打开 index.php 并编辑以下行:
index.php -> $pathsPath = FCPATH . '../app/Config/Paths.php';
改为
index.php => $pathsPath = FCPATH . 'app/Config/Paths.php';
# this is my version (Nassim) of the htaccess file , I use this one in the root directory "/"
# of the project and a second .htaccess in the public directory
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,QSA]
第二个文件(CI安装时自带)为"public/.htaccess"。
# I recommend you remove `IfModule`. Because if you need mod_rewrite,
# you don't need `IfModule`. If you don't need it, you don't need this file
# at all.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|images|assets|doc|data|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
前往 app/Config/App.php 文件,然后找到 public $indexPage = 'index.php';,将其修改为 public $indexPage = '';
操作成功
require_once './public/index.php';
好的。
pages
将位于 public/pages
。https://codeigniter.com/user_guide/tutorial/static_pages.html - AldoServerAdmin webmaster@example.com
ServerName example.com
DocumentRoot "path-to-project/public"
<Directory "path-to-project/public">
Order allow,deny
Allow from all
AllowOverride All
Require all granted
</Directory>
.htaccess
文件的。如果您从这里下载 CodeIgniter 4,您会发现 Htaccess 文件与您添加的文件不同。public/
文件夹中,它将设置为文档根目录,而在 CI 4 中放在外部的 .htaccess 文件将不再有效。
public/.htaccess
index.php
的URL问题,没有出现任何问题。没有index.php
也能正常工作/加载。 $pathsConfig = FCPATH . '../app/Config/Paths.php';
to
$pathsConfig = FCPATH . 'app/Config/Paths.php';
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
http://localhost:8080/payinlinks/index.php/login
,否则http://localhost:8080/payinlinks/login
。Your_Project_Folder/app/Config/App.php
文件$baseURL = 'http://localhost/';
[或者如果是:$baseURL = 'http://localhost:8080/';
]改为$baseURL='http://localhost/Your_Project_Name';
$indexPage = 'index.php';
改为$indexPage = '';
进入Your_Project_Folder/public
文件夹
将此文件夹中的.htaccess
和index.php
文件复制并粘贴到您的文件夹的根目录Your_Project_Folder
下(其中存在spark
或env
文件)。
打开Your_Project_Folder/index.php
文件(我们刚刚将其粘贴到项目根目录中)
更改FCPATH . '../app/Config/Paths.php';
为FCPATH . 'app/Config/Paths.php';
全部完成!!!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
3 个步骤
:
第一步:
前往此文件夹
使用您的项目文件夹名称更新App -> config -> app.php
base_url
,就像我们在 Codeigniter 3
中所做的那样。
像这样:public $baseURL = 'http://localhost/your_project_name/';
composer
安装ci 4
,则会有这种类型的base_url
,您需要将其更改为上面定义的base_url
:public $baseURL = 'http://localhost:8080';
第二步:
前往public
文件夹,复制
index.php
和 .htaccess
(记得复制而不是剪切)
然后将它们粘贴
到根目录
注意:与我们在
Codeigniter 3
中通过在根文件夹上创建.htaccess
文件并粘贴
相关代码来删除index.php
相同。
第三步:
用此代码替换根目录
文件夹中的.htaccess
文件代码:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
然后打开root
文件夹下的index.php
文件,检查第20
行或者这一行:
require FCPATH . '../app/Config/Paths.php';
然后将其中的../
移除,变成如下形式:
require FCPATH . 'app/Config/Paths.php';
public
文件夹中创建了css
和js
文件夹,并使用views/header.php
、views/footer.php
或views/index.php
上的base_url
将它们连接起来,那么可能会出现相关问题,无法获取这些文件:
当我们使用bootstrap
cdn
时,它会给出这个syntax
,我们可以像这样本地化,但由于id="bootstrap-style"
,所以会出现问题,请从此行中删除它:
<link href="<?php echo base_url();?>/public/assets/css/bootstrap.min.css" id="bootstrap-style" />
<link rel="stylesheet" href="<?php echo base_url();?>/public/assets/css/bootstrap.min.css" />
bootstrap
和css
相关的问题。