【问题标题】:Removing index.php in CodeIgniter using .htaccess file when using your own local domain使用您自己的本地域时使用 .htaccess 文件删除 CodeIgniter 中的 index.php
【发布时间】:2015-10-02 17:16:55
【问题描述】:

我看过其他关于同一问题的文章。但是没有一个答案能解决我的问题。

我没有使用 xampp 或 wamp,但已单独设置 php mysql apache。我也在使用我自己的本地域,例如 mytestsite.com。

我的 .htaccess 文件如下所示:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /TestCI/

    # Canonicalize Codeigniter URLs

    # If your default controller is something other than
    # "welcome" you should probably change this
    RewriteRule ^(HomeController(/index)?|index(\.php|html?)?)/?$ / [R=301,L]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]

    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [R=301,L]

    # Enforce www
    # If you have subdomains, you can add them to 
    # the list using the "|" (OR) regex operator
    RewriteCond %{HTTP_HOST} !^(www|TestCI) [NC]
    RewriteRule ^(.*)$ http://www.mytestsite.com/$1 [R=301,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>

    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php

</IfModule>

我还在 config.php 中做了以下更改

$config['base_url'] = 'http://mytestsite.com/TestCI/';    
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO'; //AUTO

我还在 apache2.conf 中做了以下更改

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

但是当我尝试访问时

http://mytestsite.com/TestCI/HomeController/

它显示 404 错误,但使用时可以正常工作

http://mytestsite.com/TestCI/index.php/HomeController/

我研究了几个已报告已解决的类似问题,但没有一个答案对我有用。是否应该采取其他措施从网址中删除 index.php。请回复。非常感谢您的帮助。

【问题讨论】:

  • 试试我的代码可能有帮助...
  • 仍然显示未找到错误在此服务器上找不到请求的 URL /var/www/mytestsite.com/public_html/TestCI/index.php。

标签: php .htaccess codeigniter mod-rewrite


【解决方案1】:

可能是 htaccess 没有被使用。

在文件中放置一个语法错误。 (将类似 hdtbf 的内容附加到文件中)

如果没有出现 500 错误,您需要转至 allow override all 在您的主要 Apache 配置中

http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride

【讨论】:

  • 我已经对允许覆盖所有进行了更改。还是一样
  • 你重启服务器了吗?
  • 当我在 .htaccess 文件中出现语法错误时,它确实给出了内部服务器错误。我仍然找不到问题所在
  • 是的,多次重启服务器。在做出所有这些改变之后。但如果没有 url 中的 index.php,它就无法工作。
  • 仍然显示未找到错误在此服务器上找不到请求的 URL /var/www/mytestsite.com/public_html/TestCI/index.php。
【解决方案2】:

替换.htaccess 文件中的以下代码行

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

【讨论】:

  • 进行了更改,但仍然没有成功
  • 我的答案改了,现在检查一次
  • 还是一样。 .htaccess 文件现在看起来像 RewriteEngine On RewriteBase /TestCI/ #删除用户对系统文件夹的访问。 #另外这将允许你创建一个 System.php 控制器,RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] ErrorDocument 404 /index.php
【解决方案3】:

只需将此代码放入您的 .htaccess 中即可:

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

【讨论】:

  • 你能解释一下 RewriteCond $1 吗!^(index.php|resources|robots.txt)
  • 仍然显示未找到错误在此服务器上找不到请求的 URL /var/www/mytestsite.com/public_html/TestCI/index.php。
【解决方案4】:

谢谢你帮助我...问题解决了。

我在我的 apache2.conf 文件中进行了以下更改

<Directory /Absolute path to .htaccess folder

        AllowOverride All
        Require all granted
</Directory>

我还删除了 .htaccess 文件中的 RewriteBase 参数,这是实际 pbm 所在的位置。

我当前的 .htaccess 文件看起来像

<IfModule mod_rewrite.c> 
    RewriteEngine On


    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php 
</IfModule>

我还在 config.php 中进行了更改,我将 $config['base_url'] 更改为 codeigniter 根路径

【讨论】:

    猜你喜欢
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 2011-10-09
    • 2015-03-09
    • 2017-06-01
    相关资源
    最近更新 更多