doulun1666 2015-02-01 00:27
浏览 126
已采纳

CodeIgniter .htaccess index.php重写不在用户目录上工作

I have a little problem with codeigniter, I have a .htaccess for rewrite the index.php it works great if I put my projects on /var/www or if I make a virtual host for it.

But I want to use my user directory such as http://localhost/~userdir/myproject. If I use my user directory when I'm trying to do request to a controller like: http://localhost/~userdir/myproject/signup I get this error:

Not Found

The requested URL /index.php was not found on this server.

This is my current configuration:

.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

This config works great on http://localhost/ and also on production enviroments with a domain like http://example.com/ but not on my user directory http://localhost/~userdir/project/

What I doing wrong? Any thoughts? Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dpi10335 2015-02-11 19:10
    关注

    Well, I found a solution in codeigniter forums, first I need to add RewriteBase line as follows:

    RewriteBase /~userdir/myproject/
    

    Then, remove the slash before index.php on last line.

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

    Someone said that

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

    is redundant, the following two lines of code cover it,

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

    and all other real files/directories. They basically say "if the request is not for a REAL file, and the request is not for a REAL directory, pass the request to index.php. Presumably everything from the above line is a real file or directory, so it's not needed at all.

    So, my final .htaccess is like this:

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

    It works well locally with http://localhost/~userdir/myproject also on production with http://example.com/~userdir/myproject

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失