ci url是否可能不适用于index.php

时间:2018-08-03 03:45:30

标签: php .htaccess codeigniter codeigniter-htaccess

ci url是否可能不适用于index.php。我不想在url中添加index.php。如果有人在URL中添加index.php,它应该不起作用。我尝试了很多方法。请给我建议。 我已经尝试过了,但是没有用

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

例如

如果我使用下面的网址,这应该可以..............

`example.com/abc`

如果我在下面的网址中使用应该不起作用

`example.com/index.php/abc`

8 个答案:

答案 0 :(得分:3)

在您的根目录中创建.htaccess文件,并将此代码保存在其中

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule>

打开application / config / config.php

    //find the below code   
$config['index_page'] = "index.php" 
//replace with the below code
$config['index_page'] = ""

/find the below code
$config['uri_protocol'] = "AUTO"
//replace with the below code
$config['uri_protocol'] = "REQUEST_URI" 

答案 1 :(得分:3)

在Config.php中

$config['base_url'] = 'http://localhost/website/';
$config['index_page'] = '';

# If Live site
# $config['base_url'] = 'http://stackoverflow.com/';

.htaccess

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

检查我对另一个问题的回答 Set up the base URL in codeigniter

答案 2 :(得分:2)

您可以使用:

RewriteEngine On

# 403 Forbidden for index.php
RewriteCond %{THE_REQUEST} index\.php [NC]
RewriteRule ^ - [F]

# Rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

答案 3 :(得分:1)

在index.php顶部添加以下代码。我认为这可以解决您的问题。

if(strpos($_SERVER['REQUEST_URI'],'index.php') !== false){
    header('location: http://xyx.com/404');
    die();
}

答案 4 :(得分:1)

尝试以下代码。

在config.php中更改以下内容

$config['index_page'] = ""
$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取决于服务器

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

答案 5 :(得分:0)

只需更改您的根.htaccess文件:

RewriteEngine on
RewriteBase /projectname
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

更改配置文件:

$config['index_page'] = '';

$config['uri_protocol'] = 'REQUEST_URI';

答案 6 :(得分:0)

url shouldn't work是重定向到另一个还是替换它是什么意思?

url shouldn't work-我认为您必须对用户(对搜寻器)说此链接中有错误,如果该链接已被索引,请告诉搜寻器这是404 not found页,它将停止为该页面编制索引。您无法完全禁用index.php/foo,因为所有请求都通过index.php(我相信,如果您使用的是路由)

以下是替换示例-https://htaccess.madewithlove.be?share=04dcf45c-32e9-5c29-b706-5869cd369ffd

输入网址http://ex.com/index.php/abc

RewriteEngine On
RewriteRule index.php/(.*) /404 [L] #send user to not found page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]

输出网址http://ex.com/404

答案 7 :(得分:0)

尝试以下规则,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,END]

RewriteRule ^index\.php* - [F]