【问题标题】:Remove "index.php" from URL - Codeigniter从 URL 中删除“index.php” - Codeigniter
【发布时间】:2011-10-12 04:03:30
【问题描述】:

我查看了 Codeigniter 在访问不同视图时从 URL 中删除 index.php 的文档,代码显示了如何使用 apache 将其删除:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

但是,当我转到 http://localhost/code/home 时,我得到了这个:

在此服务器上找不到请求的 URL /code/home。

但是访问http://localhost/code/index.php/home 工作正常,为什么它不适合我?

我在 Mac OS X Snow Leopard 上使用 Sites 目录:/Users/~myusername~/Sites/code,我没有使用任何软件,例如 MAMP、XAMPP。

编辑

对于sudo /usr/sbin/apachectl -k restart -S 命令,我得到了这个:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server code.local (/private/etc/apache2/httpd.conf:146)
         port 80 namevhost code.local (/private/etc/apache2/httpd.conf:146)
Syntax OK

【问题讨论】:

标签: apache macos codeigniter osx-snow-leopard


【解决方案1】:

您需要将“http://localhost/code”作为“code.local”作为您的 Web 根目录(在我的示例中)。 假设您在 Mac OS X Snow Leopard 上的设置与我的相同。

您应该将以下行添加到“/etc/apache2/extra/httpd-vhosts.conf”

<VirtualHost *:80>
    DocumentRoot "/Users/~myusername~/Sites/code"
    ServerName "code.local"
    ErrorLog "/private/var/log/apache2/code-error_log"
    CustomLog "/private/var/log/apache2/code-access_log" common
</VirtualHost>

还要确保它在“/etc/apache2/httpd.conf”中没有注释

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

然后你将 code.local 添加到你的“/etc/hosts”,它应该看起来像这样

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
127.0.0.1       code.local

然后重启你的apache“/usr/sbin/apachectl -k restart”。

如果您不是超级用户,则可能需要 sudo。 希望这会有所帮助。

编辑: 检查您的虚拟主机是否有效。运行这个 sudo /usr/sbin/apachectl -k 重启 -S 查看是否已将 code.local 添加为您的虚拟主机之一。

然后尝试访问 code.local/index.php/welcome 和 code.local/welcome 以检查它是否有效。

【讨论】:

  • 我最终得到The requested URL /code was not found on this server.
  • 啊,应该是 http://code.local/home 而不是 code.local/code,因为您的站点/代码现在是 Web 根目录。希望它现在有效。
  • 域工作,但code.local/add 不工作,但code.local/code/index.php/add 工作。我收到一个错误:The requested URL /code/add was not found on this server.
  • 啊,检查你的application/config/config.php。我只是将以下内容设置为空白。 $config['base_url'] = '';$config['index_page'] = '';
  • 顺便检查一下code.local/index.php/add是否有效。我想知道如果code.local 现在指向/Users/~myusername~/Sites/code 而不是/Users/~myusername~/Sites,为什么code.local/code/index.php/add 不应该起作用。
【解决方案2】:

试试这个

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA] 

我尝试了 3 个,然后才开始工作

【讨论】:

    【解决方案3】:

    要从 URL 中删除 index.php,只需添加

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php/$0 [PT,L]
    

    在 .htaccess 文件中并将 .htaccess 文件移动到 index.php 位置附近

    /var/www/CodeIgniter/index.php
    /var/www/CodeIgniter/.htaccess
    

    在config文件夹集中的config.php中

    $config['index_page'] = '';
    

    【讨论】:

      【解决方案4】:

      由于您的 CI 安装不在您的文档根目录中,您应该添加 RewriteBase /code/ 以使其正常工作。

      这是我用于 CI 安装的 .htacces(这比官方文档中提供的要详细一些):

      <IfModule mod_rewrite.c>
      
          RewriteEngine On
          RewriteBase /code/
      
          #Removes access to the system folder by users.
          #Additionally this will allow you to create a System.php controller,
          #previously this would not have been possible.
          #'system' can be replaced if you have renamed your system folder.
          RewriteCond %{REQUEST_URI} ^system.*
          RewriteRule ^(.*)$ /index.php?/$1 [L]
      
          #When your application folder isn't in the system folder
          #This snippet prevents user access to the application folder
          #Submitted by: Fabdrol
          #Rename 'application' to your applications folder name.
          RewriteCond %{REQUEST_URI} ^application.*
          RewriteRule ^(.*)$ /index.php?/$1 [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 index.php
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php?/$1 [L]
      </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.
          # Submitted by: ElliotHaughin
      
          ErrorDocument 404 /index.php
      </IfModule>
      

      还要确保您的 httpd 可以处理 .htaccess 文件。如果在 &lt;Directory&gt;&lt;/Directory&gt; 元素中存在带有 AllowOverride all 的行,请检查 httpd.conf

      希望有帮助

      【讨论】:

      • 不幸的是,这不起作用。我仍然不知道它不起作用。
      • 您是否检查过是否可以通过 .htaccess 覆盖 apache conf?您需要在 httpd.conf 文件中添加这样的内容:&lt;Directory /Users/&gt; AllowOverride All&lt;/Directory&gt;(需要重新启动)
      • 然后尝试使用重定向 [R] 标志的 RewriteRule 来检查 url 实际上是如何重写的(或者根本没有重写)。
      • 但它确实显示了重写后的 url,不是吗?那是什么
      【解决方案5】:

      试试……

      RewriteEngine on
      RewriteBase /code/
      RewriteCond $1 !^(index\.php|images|robots\.txt)
      RewriteRule ^(.*)$ code/index.php/$1 [L]
      

      【讨论】:

      • and I'm not using any software, i.e. MAMP, XAMPP 只是猜测您没有在 Mac 中启用指令选项(默认情况下禁用)。阅读:clagnut.com/blog/350
      • 好的,我已经进行了更改,现在我在导航到 http://localhost/code 时收到了 Forbidden - You don't have permission to access /code/ on this server.
      • /code/中的所有目录都被禁止访问。这是为什么呢?
      • 你做了什么改变?在这里分享。
      • 您应该在主配置中将AllowOverride None 更改为AllowOverride All,即httpd.conf。对于每个用户,根据我上面给出的链接,它应该是AllowOverride AuthConfig。如果在您完成该引用链接中的所有操作后一切仍然出错,那么最好的选择是安装 MAMP。
      【解决方案6】:

      我有同样的问题,事情是这样的。

      1. htacces 应该在 Codeigniter 的根目录中
      2. 在您的 htacces 文件中删除“全部拒绝”。
      3. 在上面的帖子中添加代码

      【讨论】:

        【解决方案7】:

        你应该改变

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

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

        您还需要像这样修改 $config['index_page'] 变量:

        $config['index_page'] = '';
        

        如果您愿意,请参考我在http://www.boxoft.net/2011/07/removing-index-php-from-codeigniter-2-0-2-url/ 的帖子。

        【讨论】:

        • 嗯。那也没用……我相信你的会奏效的。
        • 它在我运行 Debian 5 的 VPS 上运行良好。我稍后会在我的 Macbook 上试用它。
        【解决方案8】:

        /etc/apache2/users/.conf

        尝试添加..

        <Directory "/Users/<your user name>/Sites/">
            Options Indexes MultiViews FollowSymlinks
            AllowOverride all
            Order allow,deny
            Allow from all
        </Directory>
        

        并重新启动 Apache 以检查结果..

        【讨论】:

          【解决方案9】:

          将 .htaccess 复制到你的主 codeigniter 文件夹,我的是 localhost/codeigniter/ 然后将 {RewriteBase / 更改为 RewriteBase /codeigniter/}

          【讨论】:

            【解决方案10】:
            RewriteEngine on
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ index.php/$1 [L]
            

            使用上面的代码在你的 web 目录的根文件夹中创建一个 .htacess 文件。如果 url 链接包含 index.php,您将能够删除 index.php,这样它就会显示没有错误的页面。或者如果 url 不包含 index.php,它将显示 url 中没有 index.php 的页面。

            【讨论】:

              【解决方案11】:

              你所有的uri部分都被重写了,所以它指向/index.php/code/home。最好的方法是设置映射,例如。 http://code.local 指向您的 /Users/~myusername~/Sites/code,然后您的所有请求都在根级别解决,一切都会正常运行。

              编辑:downvoters 似乎是无知的,但没关系..

              您的代码现在执行以下操作:

              GET http://localhost/code/home
              // rewritten to:
              http://localhost/index.php/code/home
              

              所以就像我之前所说的,好的方法是主机映射,这样你就可以在根级别获得所有内容(这很好,因为你很可能会在生产服务器上以这种方式......),这样就可以了.htaccess 中没有我们的更改。

              否则你可以使用 Francesco 的建议与 RewriteBase,但有点改变:

              // Francesco's RewriteRule does this:
              http://localhost/code/home
              // rewritten to"
              http://localhost/code/index.php/code/home
              

              所以只需将 RewriteRule 更改为:

              RewriteRule ^code/(.*)$ index.php?/$1 [L]
              // rewrites in your case to:
              http://localhost/code/index.php/home
              // which is what you wanted, aye?
              

              【讨论】:

                【解决方案12】:

                根据 CodeIgniter 文档的说法

                默认情况下,index.php 文件将包含在您的 URL 中:

                example.com/index.php/news/article/my_article 您可以使用带有一些简单规则的 .htaccess 文件轻松删除此文件。以下是此类文件的示例,使用“否定”方法,其中除了指定项目之外的所有内容都被重定向:

                RewriteEngine on
                RewriteCond $1 !^(index\.php|images|robots\.txt)
                RewriteRule ^(.*)$ /index.php/$1 [L]
                

                在上面的示例中,除 index.php、images 和 robots.txt 之外的任何 HTTP 请求都被视为对 index.php 文件的请求。

                你可以在这里找到它:http://codeigniter.com/user_guide/general/urls.html

                【讨论】:

                • 我猜你没有正确阅读我的帖子,我已经在我的 htaccess 中包含了该指南,但它不起作用,因此我需要一个解决方案。
                猜你喜欢
                • 2016-05-14
                • 2014-10-28
                • 2012-06-15
                • 2012-09-21
                • 2017-07-16
                • 2014-02-28
                • 1970-01-01
                相关资源
                最近更新 更多