2012-07-14 45 views
2

我试图用CodeIgniter这些天。.htaccess隐藏index.php与相关链接问题

默认情况下,它有这样的

http://www.example.com/index.php/home 

home在URI的index.php文件是控制器

内部的功能,但我不希望index.php所以我创建.htaccess文件:

Allow from all 
RewriteEngine On 
RewriteBase/

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

RewriteCond $1 !^(index\.php|img|robots\.txt|script|css) 
RewriteRule ^(.*)$ index.php/$1 [L] 

基本上它会自动添加“index.php”,除非cond匹配。

但是这里的东西:
对于URI是这样的:http://www.example.com/home
一切工作正常。

对于这样的URI:http://www.example.com/home/
然后,我的网页中的相关链接变得疯狂。
例如: css/common.css被重定向到index.php/home/css/common.css导致未找到css文件。

日志是这样的:

strip per-dir prefix: [dir to my site]/home/css/common.css -> home/css/common.css 
applying pattern '^(.*)$' to uri 'home/css/common.css' 
rewrite 'home/css/common.css' -> 'index.php/home/css/common.css' 
add per-dir prefix: index.php/home/css/common.css -> [dir to my site]/index.php/home/css/common.css 
trying to replace prefix [dir to my site]/ with/
internal redirect with /index.php/home/css/common.css [INTERNAL REDIRECT] 
add path info postfix: [dir to my site]/index.php -> [dir to my site]/index.php/home/css/common.css 
strip per-dir prefix: [dir to my site]/index.php/home/css/common.css -> index.php/home/css/common.css 
applying pattern '^(.*)$' to uri 'index.php/home/css/common.css' 
pass through [dir to my site]/index.php 

我该如何解决这个问题?

+0

+1添加日志! – Ansari 2012-07-14 00:44:25

回答

2

这里的问题不是你的重写规则 - 它们很好。发生了什么事是浏览器认为/home/最后一个斜杠是一个真实的目录,因此它将所有相关链接前缀到它认为它所在的目录中。浏览器然后破坏对CSS文件的请求,路径为/home/css/common.css,它自然会被重写,因为它不符合您的支票。

我在这里看到的最快捷的解决方案是为CSS文件使用绝对路径。只需将它们链接为/css/common.css,而不是现在的相关链接。

+0

其实我在网站中使用了很多类似的链接。只要所有链接都是从我的网页生成的,它们都可以。那么我可以忽略这个问题吗? – NSF 2012-07-14 01:16:41

+0

我不知道你是否可以忽略它 - 如果它找不到CSS文件,是不是受影响? – Ansari 2012-07-14 01:36:58

+0

是的,但只要链接是由我的网页生成的,他们将不会包含最后一个斜杠,看起来工作正常 – NSF 2012-07-14 07:10:37