【问题标题】:Htaccess mod_rewrite blocking my cssHtaccess mod_rewrite 阻止我的 CSS
【发布时间】:2015-11-25 21:54:13
【问题描述】:

让 htaccess 重写我的链接以获得更好的 seo,如下所示:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  .*/([^/]+)/?     [NC]
RewriteCond %{REQUEST_URI}  !index\.php      [NC]
RewriteRule  .*      /index.php?cat=%1 [L,NC,QSA]

重写

http://www.example.com/any/number/of/directories/lastDir

到:

http://www.example.com/index.php?cat=lastDir

但是我的 css 不工作,当我将 htaccess 上传到服务器时,只有纯文本没有图像和 css

尝试将base标签添加到html中,但它不起作用

<base href="http://www.example.com/">

【问题讨论】:

  • 尝试改用.*/([^/.]+)/?。您的规则可能与 css 和图像文件匹配。不允许重写中的点会阻止这种情况,因为文件扩展名包含一个点。
  • 不适用于 example.com/aaaa,仅适用于 example.com/aaa/bbb 等

标签: css .htaccess mod-rewrite


【解决方案1】:

您必须排除 css 和图像被 RewriteCond 重写

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(?:css|png|jpe?g|gif)$ [NC,OR]
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ([^/]+)/?$ /index.php?cat=$1 [QSA,L]

【讨论】:

  • +1 for jpe?g,以前从未想过,我一直使用jpeg|jpg,并且一直在绞尽脑汁想要更整洁的东西
  • @bizzehdee 谢谢,我从其他地方采用了它。不过,jpeg|jpg 没什么问题。
【解决方案2】:

尝试使用

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  .*/([^/]+)/?     [NC]
RewriteCond %{REQUEST_URI} !^(index\.php|robots\.txt|img|font|js|css|scripts) [NC]
RewriteRule  .*      /index.php?cat=%1 [L,NC,QSA]

这将过滤掉某些文件类型和目录,以确保资产不会被重定向

【讨论】:

    【解决方案3】:

    我也遇到了这个问题,找到了解决办法。

    如果你的 css / js 文件是用相对路径链接的,你的 RewriteRule 也会影响它们。您可以通过使用根目录中的路径来避免这种情况,而不是编写 RewriteCond。换句话说:

    <link type="text/css" rel="stylesheet" href="css/style.css">
    

    变成

    <link type="text/css" rel="stylesheet" href="/css/style.css">
    

    其中css是根目录下的一个目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      • 2012-08-04
      • 2012-07-25
      • 1970-01-01
      相关资源
      最近更新 更多