【问题标题】:Loading images after htaccess rewritehtaccess 重写后加载图像
【发布时间】:2013-11-01 23:01:48
【问题描述】:

首先为写得不好的标题道歉。无法一语道破这个问题。

我有一个简单的 htaccess 规则设置有一些花哨的 url。我还使用它来使用控制器组合和加载我的所有 css 文件。

htaccess如下

RewriteEngine on

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

RewriteRule ^(style\.css) Asset [L]

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

index.php 是一个简单的路由路由到正确的控制器,正如您所见,当 style.css 被调用时,它将请求传输到资产控制器。然后我遇到的问题是如何使用 css 文件加载图像。重写条件也适用于图像,这是我想要避免的。

有人遇到过类似的问题吗?

提前非常感谢任何帮助。

【问题讨论】:

  • 你的意思是浏览器请求图片的时候实际执行的是index.php?
  • 是的。所有图像请求都转到 index.php?rt=$1。我不想那样
  • 打开浏览器的开发者工具,查看浏览器实际调用了哪些URL,可能图片不存在?
  • 没有伙伴。我想你误解了我的意思。图像确实存在。对 www.mysite.com/img.jpg 的标准图像请求转到 www.mysite.com/index.php?rt=img.jpg。这就是问题
  • 如果您禁用 rewriteengine 并尝试打开图像 - 它会起作用吗?

标签: apache .htaccess mod-rewrite url-rewriting rewrite


【解决方案1】:

将 .htaccess 更改为

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1&%{QUERY_STRING} [L]

在控制器中检查$_GET['rt']中是否为style.css

如果您想在 mod_rewrite 级别将 style.css 更改为 Asset,请使用:

RewriteEngine on
RewriteCond %{REQUEST_URI} style\.css
RewriteRule .* index.php?rt=Asset&%{QUERY_STRING} [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1&%{QUERY_STRING} [L]

Options -Indexes # this line is optional

【讨论】:

  • 嘿,走近了。除了css之外,一切都很好。所以我终于可以通过键入图像文件夹来遍历文档结构。控制器加载也很好。但是,当请求 style.css (localhost:8888/test/style.css) 时,它会将这个请求作为 style.css 发送到 index.php。换句话说 $1 是 style.css 但我希望它是 Asset
  • 从条件开始删除^并重写规则
  • 我通过删除此行使其工作。 RewriteCond %{REQUEST_FILENAME} ^style\.css 我如何在资产之上传递一些额外的参数。即:我的网址看起来像这样:style.css?p=library&t=css.. 现在它只是将请求发送到 index.php?rt=Asset... 我如何传递其他参数?非常感谢您的帮助
  • 你的意思是 style.css 也应该通过 index.php 吗?所以只有图像应该避免重写?
  • 对不起,又把你弄糊涂了。想要 style.css 去 Asset 但还有其他参数?p=library&t=css
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 2017-05-19
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
相关资源
最近更新 更多