.htaccess重定向自定义错误页面并从URL中删除index.php

时间:2018-09-11 06:09:14

标签: php .htaccess

我有这段代码可以从URL中删除index.php并重定向到自定义错误页面。 index.php从URL删除可以正常工作,但我不知道如何与此一起重定向自定义错误页面代码。

我已经尝试过此代码,但无法正常工作

RewriteEngine on
<br>RewriteCond %{REQUEST_FILENAME} !-f<br>
RewriteCond %{REQUEST_FILENAME} !-d <br>
RewriteRule ^(.*)$ index.php?/$0 [QSA,L]<br>

ErrorDocument 400 /errors.php
ErrorDocument 403 /errors.php
ErrorDocument 404 /errors.php
ErrorDocument 405 /errors.php
ErrorDocument 408 /errors.php
ErrorDocument 500 /errors.php
ErrorDocument 502 /errors.php
ErrorDocument 504 /errors.php

两者都有解决方案吗?

1 个答案:

答案 0 :(得分:0)

在config.php文件中进行一些更改转到“您的项目或域/应用程序/config/config.php”

您将在下面找到代码。

$config['index_page'] = 'index.php';
//Replace above code with below code
$config['index_page'] = '';

已完成,但是如果仍然遇到问题,则需要在config.php文件中再更改一个配置。  您需要找到以下代码

$config['uri_protocol'] = 'AUTO'; 
//Replace above code with below code
$config['uri_protocol'] = 'REQUEST_URI';

.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

注意:.htaccess代码因托管服务器而异。在某些托管服务器(例如:Godaddy)中,是否需要额外使用?在上述代码的最后一行。在适用的情况下,以下行将替换为最后一行:

// Replace last .htaccess line with this line
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]