如何强制.htaccess用于路由到不路由.css,.js,.jpg等文件?

Edw*_*uay 12 php .htaccess

我在站点的子目录中有以下.htaccess文件,它允许我将所有URL路由到index.php,我可以在其中解析它们.

但是,它不允许我需要的网站标准文件,例如css,javascript,pngs等.

我需要更改(我假设在第四行)允许这些文件,以便它们不会被路由到index.php?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|png|jpg|gif|robots\.txt)
RewriteRule ^(.*)$ index.php/params=$1 [L,QSA]
ErrorDocument 404 /index.php
Run Code Online (Sandbox Code Playgroud)

Abs*_*ERØ 12

我注意到了什么.你正在使用正斜杠而不是问号... params重定向通常如下所示:

RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
Run Code Online (Sandbox Code Playgroud)

这应该是独立的,因为任何这些文件*应该是真实的文件.

ErrorDocument 404 /index.php    

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
Run Code Online (Sandbox Code Playgroud)

要让站点忽略特定扩展,您可以添加条件以忽略大小写,并仅检查请求中文件名的结尾:

RewriteEngine On

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
Run Code Online (Sandbox Code Playgroud)

如果您尝试忽略文件夹,则可以添加:

RewriteEngine On

RewriteCond %{REQUEST_URI} !(public|css)
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
Run Code Online (Sandbox Code Playgroud)


Den*_*rdy 6

最简单的方法是在规则的早期明确地忽略它们:

RewriteRule \.(css|js|png|jpg|gif)$ - [L]
RewriteRule ^(index\.php|robots\.txt)$ - [L]
Run Code Online (Sandbox Code Playgroud)

这可以避免使用RewriteCond将它们带到各处.

根据您的选择,在执行此操作之前检查文件是否存在:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif)$ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(index\.php|robots\.txt)$ - [L]
Run Code Online (Sandbox Code Playgroud)

(请注意,文件检查会生成额外的磁盘访问权限.)