如何强制用于路由的 .htaccess 不路由 .css、.js、.jpg 等文件? [英] How to force .htaccess used for routing to not route .css, .js, .jpg, etc. files?
问题描述
我在一个站点的子目录中有以下 .htaccess 文件,它允许我将所有 URL 路由到 index.php,在那里我可以解析它们.
I have the following .htaccess file in a subdirectory of a site which allows me to route all URLs to index.php where I can parse them.
但是,它不允许使用网站所需的标准文件,例如css、javascript、png 等.
However, it's not allowing the standard files that I need for the website, e.g. css, javascript, pngs, etc.
我需要更改什么(我假设在第四行)以允许这些文件,以便它们不会被路由到 index.php?
What do I need to change (I assume in the fourth line) to allow these files so they don't get routed to 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
推荐答案
我注意到的一些事情.您使用的是正斜杠而不是问号... params 重定向通常如下所示:
Something I noticed. You're using the forward slash instead of a question mark... the params redirect would normally look like this:
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
这应该自己工作,因为这些文件中的任何一个*应该*是真实的文件.
This should work by itself since any of those files *should* be real files.
ErrorDocument 404 /index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
要让站点忽略特定扩展名,您可以添加一个条件来忽略大小写并仅检查请求中文件名的结尾:
To have the site ignore specific extensions you can add a condition to ignore case and only check the end of the filenames in the request:
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]
如果你想忽略一个文件夹,那么你可以添加:
If you're trying to ignore a folder then you could add:
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]
这篇关于如何强制用于路由的 .htaccess 不路由 .css、.js、.jpg 等文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!