Htaccess breaks css and javascripts

旧街凉风 提交于 2020-01-05 03:54:06

问题


RewriteRule ^([^/]*)/([^/]*)$ index.php?sayfa=$1&id=$2 [L]

i have an htaccess code like this but it breaks css, javascripts and images. something happens like this

sayfa = css

id = style.css


回答1:


I would suggest just checking if the file exists using a rewrite condition:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ index.php?sayfa=$1&id=$2 [L]

This essentially says, if the requested filename does not actually exist on disk and is not a directory that exists on disk, then try to apply the rule.




回答2:


Use a condition before that rule to confirm the request isn't for js or css files.

RewriteCond %{THE_REQUEST} !(?:css|js)$
RewriteRule ^([^/]*)/([^/]*)$ index.php?sayfa=$1&id=$2 [L]

Regex demo: https://regex101.com/r/7WPKGh/1/

The ?: is a non-capture group. The | are alternations. The $ is the end of the string.

For jpeg, jpg, gif, and pngs it could be updated to:

RewriteCond %{THE_REQUEST} !(?:css|js|jpe?g|png|gif)$
RewriteRule ^([^/]*)/([^/]*)$ index.php?sayfa=$1&id=$2 [L]


来源:https://stackoverflow.com/questions/43105908/htaccess-breaks-css-and-javascripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!