防止 CSS URL 被 htaccess 重写


Prevent CSS URL from htaccess rewrite

我正在创建一个标准的重写,.htaccess来做到这一点

http://www.myurl.com/post/{id}/

RewriteRule    ^post/([0-9]+)/?$    post.php?id=$1    [NC,L]

现在,它运行完美,但所有其他外部URL(例如CSS和JavaScript)都不起作用,正确。有没有办法防止它而不必更改所有网址?我在谷歌上搜索没有成功,有什么想法吗?

您将需要一个额外的规则来修复css,js,图像链接:

# your rewrite rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/([0-9]+)/?$ post.php?id=$1 [QSA,NC,L]
# fix css, js, image links
RewriteRule ^post/(.+?'.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /$1 [NC,L]

或者,确保在css,js,图像文件中只使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以http://或斜杠开头/

只需从您的条件中排除物理文件

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule    ^post/([0-9]+)/?$    post.php?id=$1    [NC,L]