Reputation: 23151
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !=cms
RewriteRule ^([^/]*)/search/([^/]+)$ index.php?lang=$1&id=search&searchword=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?lang=$1&id=$2 [L]
It doesn't work as i expect.
My .css
,.js
files has absolute path, like http://bs.am/finance/css/style.css
(my all files in finance
folder)
The first rule breaks all css and js files, but if i replace the places of rewriteRules, works fine.
Could somebody explain such behavior?
I also tried RewriteCond %{REQUEST_FILENAME} !^(.+)\.css$
but doesn't help.
Thanks much
Upvotes: 3
Views: 3075
Reputation: 19169
Based on your question, it seems that your .htaccess file is in the /finance/ folder. If this is the case, the problem lies in the fact that your second RewriteRule
will match the requests for your scripts and sylesheets.
The conditions you have already will allow you to avoid this problem, but they only apply to the first rule that comes after them (your search rule). As usual, there are a few different ways to fix things, but the simplest is to copy your RewriteCond
block so that it applies to the second rule too. This would look like the following:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 =cms
RewriteRule ^([^/]*)/search/([^/]+)$ index.php?lang=$1&id=search&searchword=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !=cms
RewriteRule ^([^/]*)/([^/]*)$ index.php?lang=$1&id=$2 [L]
Note that you might not even need the condition block for the first rule, as it's probably unlikely that any of your real files match that test pattern. It is definitely needed on the second one though, to prevent your resources from being misdirected.
Alternative Approach
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond $1 !=cms
RewriteRule ^([^/]*) - [S=2]
RewriteRule ^([^/]*)/search/([^/]+)$ index.php?lang=$1&id=search&searchword=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?lang=$1&id=$2 [L]
In this approach, if any of your conditions match, we skip the next two rules (indicated by the S=2
. This is the same as applying the condition block to both of them. As Gumbo mentions, you could also use the L
flag in place of the S=2
to ignore every rule that comes after that block in the case of your conditions matching. Which of these options is most appropriate depends on what other rules you might want to add in the future.
Upvotes: 2
Reputation: 655469
A RewriteCond
directive does only belong to the next following RewriteRule
. So your three RewriteCond
directives do only belong to the first RewriteRule
but not to the second that has no conditions.
So to exclude existing files and directories for the second RewriteRule
, you would need to write the same RewriteCond
s in front of your other rule too:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !=cms
RewriteRule ^([^/]*)/search/([^/]+)$ index.php?lang=$1&id=search&searchword=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !=cms
RewriteRule ^([^/]*)/([^/]*)$ index.php?lang=$1&id=$2 [L]
But you can also use a rule that stops the rewriting process when the request can be mapped onto an existing file or directory:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
Put this rule in front of your other rules and the rewriting process will stop with this rule.
Upvotes: 1