在 htaccess 中使用斜线会使 css 个文件无法加载

using slash in htaccess makes css files not to load

首先,我花了 3 个晚上试图找到这个问题的答案 :) 如果有这个问题的答案,我找不到或无法理解。

我的问题很简单:

我想用斜杠拆分获取参数。并在我的脚本中全局使用它。

在 htaccess 中,我写了这个;

RewriteRule ^(.*)/(.*) index.php?param1=&param2= [NC,QSA,L]
RewriteRule ^(.*) index.php?param1= [NC,QSA,L]

但这会阻止(或类似的)css 文件。当浏览器加载页面时,css 文件链接看起来正常但浏览器无法访问它们(因为 htaccess 获取目录作为参数)

感谢您的帮助

编辑:我的完整 .htaccess:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f


RewriteRule ^(.*)/(.*) index.php?param1=&param2= [NC,QSA,L]
RewriteRule ^(.*) index.php?param1= [NC,QSA,L]

通过将其置于规则之上:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

它会检查文件是否已经存在(例如 CSS 文件),因此不会尝试重写或重定向它们。

确保您的两个规则都像这样。条件仅适用于它们之后的第一个重写规则。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*) index.php?param1=&param2= [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?param1= [NC,QSA,L]

也为您的 CSS 文件使用绝对路径或在文件路径前放置 /