【问题标题】:Mod_Rewrite - CSS and Images not foundMod_Rewrite - 未找到 CSS 和图像
【发布时间】:2015-06-02 22:26:24
【问题描述】:

我知道这是一个非常简单的问题,但我现在真的很困惑。我尝试了几个选项,但结果都是一样的:CSS file is not found if I use mod_rewrite

解决方案应包含的内容:

  1. domain.tld -> www.domain.tld
  2. www.domain.tld/category/page-title -> index.php?a=category&b=page-title
  3. www.domain.tld/category/page-title/ -> index.php?a=category&b=page-title
  4. www.domain.tld/category -> index.php?a=category
  5. www.domain.tld/category/ -> index.php?a=category

.htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.tld
RewriteRule (.*) http://www.domain.tld/$1 [R=301,L]

RewriteEngine on
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(.*)/(.*)/?$ index.php?a=$1&b=$2
RewriteRule ^(.*)/?$ index.php?a=$1 [L]

index.php:

<link href="/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/internal.css" rel="stylesheet">

【问题讨论】:

  • 其他一切是否也如您所愿?具体点 1 到 5?

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

问题是您的最后一个重写规则在没有 RewriteCond 的情况下运行以跳过真实文件/目录,甚至将 js/css/image 文件路由到 index.php

像这样:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.tld
RewriteRule (.*) http://www.domain.tld/$1 [R=301,L]

# skip all files and directories from rules below
RewriteCond %{REQUEST_fileNAME} -d [OR]
RewriteCond %{REQUEST_fileNAME} -f
RewriteRule ^ - [L]

RewriteRule ^(.*)/(.*)/?$ index.php?a=$1&b=$2 [L,QSA]

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

【讨论】:

  • 就是这样!非常感谢!
猜你喜欢
  • 2012-08-04
  • 2014-07-19
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 2019-06-13
  • 2012-07-05
  • 2010-10-07
  • 2017-09-29
相关资源
最近更新 更多