【问题标题】:CodeIgniter .htaccess index.php rewrite not working on user directoryCodeIgniter .htaccess index.php 重写对用户目录不起作用
【发布时间】:2015-03-31 05:47:30
【问题描述】:

我对 codeigniter 有一点问题,我有一个 .htaccess 用于重写 index.php,如果我把我的项目放在 /var/www 上或者我为它创建一个虚拟主机,它会很好用。

但我想使用我的用户目录,例如http://localhost/~userdir/myproject。如果我在尝试向控制器发出请求时使用我的用户目录,例如:http://localhost/~userdir/myproject/signup,我会收到此错误:

没有找到

在此服务器上找不到请求的 URL /index.php。

这是我当前的配置:

.htaccess

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

application/config/config.php

$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['encryption_key'] = 'myencryption_key';
//all remaining is the default config

此配置在http://localhost/ 以及具有http://example.com/ 之类的域但不适用于我的用户目录http://localhost/~userdir/project/ 的生产环境中效果很好

我做错了什么?有什么想法吗? 提前致谢。

【问题讨论】:

    标签: php apache .htaccess codeigniter mod-rewrite


    【解决方案1】:

    嗯,我在codeigniter forums找到了解决方案,首先我需要添加RewriteBase行如下:

    RewriteBase /~userdir/myproject/
    

    然后,删除最后一行 index.php 之前的斜线。

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

    有人说

    RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favico​​n\.ico)
    

    是多余的,下面两行代码覆盖它,

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    

    以及所有其他真实文件/目录。他们基本上说“如果请求不是针对 REAL 文件,并且请求不是针对 REAL 目录,则将请求传递给 index.php。大概上一行中的所有内容都是真实文件或目录,因此不需要全部。

    所以,我最终的 .htaccess 是这样的:

    RewriteEngine on
    RewriteBase /~userdir/myproject/   
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
    

    它在本地与http://localhost/~userdir/myproject 一起工作得很好,也与http://example.com/~userdir/myproject 一起在生产中工作

    【讨论】:

    【解决方案2】:

    本地解决方案

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

    在线解决方案 RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?/$1 [L]

    【讨论】:

      【解决方案3】:

      你需要像这样删除 index.php 前面的第一个斜杠

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

      【讨论】:

      • 这对我不起作用,我明白了:在此服务器上找不到请求的 URL /home/userdir/public_html/project/index.php。
      • 抱歉,我没有看到您从配置中删除了 index.php。尝试把 $config['index_page'] = 'index.php';
      猜你喜欢
      • 1970-01-01
      • 2017-02-21
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 2014-01-09
      • 1970-01-01
      相关资源
      最近更新 更多