gpt4 book ai didi

html - .htaccess 隐藏具有相对链接问题的 index.php

转载 作者:行者123 更新时间:2023-11-28 00:17:19 26 4
gpt4 key购买 nike

最近我在尝试使用 CodeIgniter。

默认情况下它在 URI 中有这样的 index.php

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

home 是 Controller 内部的一个函数

但是我不想要 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”,除非条件匹配。

然而事情是这样的:
对于这样的 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

我该如何解决这个问题?

最佳答案

这里的问题不是你的重写规则——它们很好。发生的事情是浏览器认为 /home/ 末尾有一个斜杠是一个真正的目录,所以它在所有相关链接前加上它认为它所在的目录。然后浏览器冲掉一个请求CSS 文件,路径为 /home/css/common.css,由于它与您的检查不匹配,因此自然会被重写。

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

关于html - .htaccess 隐藏具有相对链接问题的 index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11479888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com