简体   繁体   English

CodeIgniter .htaccess index.php重写不适用于用户目录

[英]CodeIgniter .htaccess index.php rewrite not working on user directory

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. 我的codeigniter有一个小问题,我有一个.htaccess来重写index.php,如果我将项目放在/ var / www上或者为它创建虚拟主机,那么它工作得很好。

But I want to use my user directory such as http://localhost/~userdir/myproject . 但是我想使用我的用户目录,例如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: 如果在尝试向类似http:// localhost /〜userdir / myproject / signup的控制器发出请求时使用用户目录, 则会出现此错误:

Not Found 未找到

The requested URL /index.php was not found on this server. 在此服务器上找不到请求的URL /index.php。

This is my current configuration: 这是我当前的配置:

.htaccess 的.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.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/ 此配置在http:// localhost /以及域为http://example.com/的生产环境中都很好用,但在我的用户目录http:// localhost /〜userdir / project /上却不行

What I doing wrong? 我做错了什么? Any thoughts? 有什么想法吗? Thanks in advance. 提前致谢。

Well, I found a solution in codeigniter forums , first I need to add RewriteBase line as follows: 好吧,我在codeigniter论坛中找到了一个解决方案,首先我需要添加RewriteBase行,如下所示:

RewriteBase /~userdir/myproject/

Then, remove the slash before index.php on last line. 然后,在最后一行的index.php之前删除斜杠。

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. 他们基本上说:“如果请求不是针对REAL文件,并且该请求并非针对REAL目录,请将请求传递给index.php。大概上一行中的所有内容都是真实文件或目录,因此不需要所有。

So, my final .htaccess is like this: 所以,我最终的.htaccess是这样的:

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 它在本地使用http:// localhost /〜userdir / myproject也可以很好地在生产中使用http://example.com/~userdir/myproject

您需要像这样删除第一个斜杠berfore index.php

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

The Local Solution 本地解决方案

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM