【问题标题】:Is it possible that ci url will not work with index.phpci url 是否可能不适用于 index.php
【发布时间】:2019-01-10 21:33:40
【问题描述】:

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]

例如

如果我使用下面的 url 这应该可以........

`example.com/abc`

如果我使用下面的网址不应该工作

`example.com/index.php/abc`

【问题讨论】:

  • 请查看以下链接。

    点击[stackoverflow.com/questions/19183311/…]
  • 如果您想要简单的解决方案,只需将index.php 文件重命名为其他名称,并将RewriteRule 修改为指向该文件,这比编写复杂的不必要的重写条件要好。

标签: php codeigniter .htaccess


【解决方案1】:

在你的根目录中创建 .htaccess 文件并将这段代码保存在其中

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

打开应用程序/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" 

【讨论】:

    【解决方案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】:

      在 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

      【讨论】:

      • 我不知道为什么这个答案被赞成。没有阅读问题,你已经给出了答案
      【解决方案4】:

      试试下面的代码...

      在 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】:

        在 index.php 的顶部添加以下代码。我想这会解决你的问题。

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

        【讨论】:

          【解决方案6】:

          试试下面的规则,

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

          【讨论】:

            【解决方案7】:

            你说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

            【讨论】:

            • 查看我的回答中的链接
            • 会发生什么。在管理区域我使用 index.php 。那怎么样?
            • 我编辑了我的答案,在我的路由器上对其进行了测试,当 url 类似于 ex.com/index.php/foo/bar/baz 时,它将请求 ex.com/404 页面,但当 url 类似于 ex.com/foo/bar/baz 时,它将请求ex.com/index.php/foo/bar/baz。所以在这两种情况下,url都会通过index.php请求页面,我认为这是正确的。我相信你有一个进入该网站的点。试一试应该可以的
            • 这取决于你的路由系统。
            【解决方案8】:

            只需更改您的根 .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';
            

            【讨论】:

            • 你在本地工作吗?您使用哪个 xamp 或 wamp ?它仅适用于 xamp,如果网站在服务器上,我的代码肯定会工作。
            猜你喜欢
            • 2014-07-07
            • 2013-12-20
            • 1970-01-01
            • 1970-01-01
            • 2013-11-09
            • 1970-01-01
            • 1970-01-01
            • 2013-12-14
            • 1970-01-01
            相关资源
            最近更新 更多