Mod_Rewrite - 找不到CSS和图像

时间:2015-06-02 22:26:25

标签: php regex apache .htaccess mod-rewrite

我知道这是一个非常简单的问题,但我现在真的很困惑。我尝试了几个选项,但结果是一样的:如果我使用mod_rewrite

,则找不到CSS文件

解决方案应包含的内容:

  1. domain.tld - >是www.domain.tld
  2. www.domain.tld / category / page-title - > ?的index.php一个类别=和b =页标题
  3. www.domain.tld / category / page-title / - > ?的index.php一个类别=和b =页标题
  4. www.domain.tld / category - >的index.php?一个类别=
  5. www.domain.tld / category / - >的index.php?一个类别=
  6. 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 个答案:

答案 0 :(得分: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]