除资产外,如何使用mod_rewrite重定向到index.php?

时间:2023-01-13 11:19:17

I would like to have mod_rewrite go into effect for any filenames that don't exists except if that file name ends with js, css, gif, etc., so those would return regular 404s...

我想mod_rewrite对任何不存在的文件名生效,除非该文件名以js,css,gif等结尾,所以那些将返回常规404 ...

I tried this out:

我试过这个:

RewriteCond %{REQUEST_FILENAME} \.(js|ico|gif|jpg|png|css|pdf)$ [OR]
RewriteCond %{REQUEST_FILENAME} favicon.ico$ [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php/$1 [L]

Which I found online somewhere, but it doesn't seem to work. My non existant JS files still get routed to my index.php file.

这是我在网上找到的,但似乎没有用。我的不存在的JS文件仍然被路由到我的index.php文件。

3 个解决方案

#1


Try this rule:

试试这条规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !.*\.(js|ico|gif|jpg|png|css|pdf)$ /index.php%{REQUEST_URI} [L]

#2


Have you added the commands that go before the .htaccess file? Like this line?

您是否添加了.htaccess文件之前的命令?喜欢这条线?

RewriteEngine On

#3


You might want to use the [NC] flag on the first line if you can have not only .js files but also .Js or .JS or .jS files...

如果您不仅可以使用.js文件,还可以使用.Js或.JS或.jS文件,则可能需要在第一行使用[NC]标志...

#1


Try this rule:

试试这条规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !.*\.(js|ico|gif|jpg|png|css|pdf)$ /index.php%{REQUEST_URI} [L]

#2


Have you added the commands that go before the .htaccess file? Like this line?

您是否添加了.htaccess文件之前的命令?喜欢这条线?

RewriteEngine On

#3


You might want to use the [NC] flag on the first line if you can have not only .js files but also .Js or .JS or .jS files...

如果您不仅可以使用.js文件,还可以使用.Js或.JS或.jS文件,则可能需要在第一行使用[NC]标志...