问题
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 png
s 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