【问题标题】:not rewrite css or js or image files htaccess不重写 css 或 js 或图像文件 htaccess
【发布时间】:2016-04-28 14:25:02
【问题描述】:

我有一个这样的网址:

mysite.com/1/everything

我想重写它并且只获取 url 中的数字(例如上面的 1)。为此,我写了一个这样的htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule for root pages
RewriteRule (^\d+\/?([\x00-\x7F]|[^\x00-\x7F])+\/?$) index-continue.php?id=$1 [NC,L,QSA]

但是我所有的css 文件、图像文件和 js 文件都被重写了!我怎么解决这个问题?这会导致所有文件在错误的路径上重写。

【问题讨论】:

    标签: php css apache .htaccess mod-rewrite


    【解决方案1】:

    你的 .htaccess 文件应该只保留以下内容,它将允许你的 js 和 css 直接使用路径 root/css/file.css 和 root/js/file.js 打开

    RewriteEngine On
    RewriteRule ^style/([^/.]+)?$ style/$1
    RewriteRule ^js/([^/.]+)?$ js/$1
    RewriteRule ^([^/.]+)/([^/.]+)?$ index-continue.php?id=$1&%{QUERY_STRING}
    

    【讨论】:

    • [^/.]+ 否认 / 和 .在字符串中!我的 css 文件位于 style/app.css 路径中,因此出现 404 错误,(我已将 css 更改为 style 但未解决 404)
    • 我已经编辑了答案,检查它是否有效?
    • 我自己已经解决了,非常感谢亲爱的。结果是:
    • RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^/.]+)/([^/.]+)?$ index-continue .php?id=$1 [NC,L,QSA] RewriteRule ^([^/.]+)/styles/(.+)?$ /mysite/styles/$2 [NC,L,QSA]
    【解决方案2】:

    要修复重写后的 url 上的 css 和 js 路径,您需要在网页的 head 部分添加以下基本标记:

    <base href="/">
    

    相关:

    Seo Friendly Url css img js not working

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2015-04-21
      • 2021-03-13
      • 2014-01-10
      • 2016-03-28
      相关资源
      最近更新 更多