CodeIgniter .htaccess index.php 重写在用户目录上不起作用
CodeIgniter .htaccess index.php rewrite not working on user directory
我对 codeigniter 有点问题,我有一个 .htaccess 用于重写 index.php 如果我将我的项目放在 /var/www 上或者为它创建一个虚拟主机,它会很好用。
但是我想使用我的用户目录,例如 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 我得到这个错误:
Not Found
The requested URL /index.php was not found on this server.
这是我当前的配置:
.htaccess
RewriteEngine on
RewriteCond !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/ [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/ and also on production enviroments with a domain like http://example.com/ but not on my user directory http://localhost/~userdir/project/
上运行良好
我做错了什么?有什么想法吗?
提前致谢。
您需要像这样删除 index.php 前的第一个斜杠
RewriteRule ^(.*)$ index.php?/ [L,QSA]
本地解决方案
RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /ci/index.php/ [L]
在线解决方案
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/ [L]
好吧,我在codeigniter forums中找到了解决方案,首先我需要添加如下RewriteBase行:
RewriteBase /~userdir/myproject/
然后,删除最后一行 index.php 前的斜杠。
RewriteRule ^(.*)$ index.php?/ [L,QSA]
有人说
RewriteCond !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
是多余的,下面两行代码覆盖,
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.
所以,我最后的 .htaccess 是这样的:
RewriteEngine on
RewriteBase /~userdir/myproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L,QSA]
它在本地与 http://localhost/~userdir/myproject also on production with http://example.com/~userdir/myproject
配合得很好
我对 codeigniter 有点问题,我有一个 .htaccess 用于重写 index.php 如果我将我的项目放在 /var/www 上或者为它创建一个虚拟主机,它会很好用。
但是我想使用我的用户目录,例如 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 我得到这个错误:
Not Found
The requested URL /index.php was not found on this server.
这是我当前的配置:
.htaccess
RewriteEngine on
RewriteCond !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/ [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/ and also on production enviroments with a domain like http://example.com/ but not on my user directory http://localhost/~userdir/project/
上运行良好我做错了什么?有什么想法吗? 提前致谢。
您需要像这样删除 index.php 前的第一个斜杠
RewriteRule ^(.*)$ index.php?/ [L,QSA]
本地解决方案
RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /ci/index.php/ [L]
在线解决方案
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/ [L]
好吧,我在codeigniter forums中找到了解决方案,首先我需要添加如下RewriteBase行:
RewriteBase /~userdir/myproject/
然后,删除最后一行 index.php 前的斜杠。
RewriteRule ^(.*)$ index.php?/ [L,QSA]
有人说
RewriteCond !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
是多余的,下面两行代码覆盖,
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.
所以,我最后的 .htaccess 是这样的:
RewriteEngine on
RewriteBase /~userdir/myproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L,QSA]
它在本地与 http://localhost/~userdir/myproject also on production with http://example.com/~userdir/myproject
配合得很好