Answer the question
In order to leave comments, you need to log in
How to specify mod_rewrite exceptions to which the rule should not be applied?
I need to create virtual root directories for users.
Well, that is, rewrite all queries like:
example.com <user_name>
to
example.com/user?name= <user_name>
and I figured out how to write such a template:
RewriteRule ^/([A-Za-z0-9_- ]+)/?$ /user?name=$1 [PT,L]
but how to make sure that mod_rewrite doesn't overwrite directories that actually exist in the root?
for example /css, /js, /img, and there are a lot of scripts besides /user
Specify all of them in the regular expression pattern as exceptions (something like: [^css|^js|^img…]), I I don’t think it’s right, because there will be more than a dozen exceptions ...
UPD: In general, in my case, it seems that there is a specificity ...
I use Apache in conjunction with Tomcat,
via
mod_jk
mod_jk
is
configured
like
this
: /* ajp13
Therefore, static files that Apache itself gives (.html, .ico, .css etc) - they can be excluded in the way that was already suggested in the first comment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
but requests to mod_rewrite servlets will still be rewritten.
Apache doesn't know if they exist or not, it just gives them away to tomcat.
Therefore, along with the correct behavior:
example.com <user_name> --> example.com/user?name= <user_name>
will be present and incorrect:
example.com/login --> example.com/user?name=login
(login is a servlet)
Like everyone else is it possible to solve this problem?
UPD2: In general, the solution turned out to be simple:
RewriteCond %{REQUEST_URI} !=/login
RewriteCond %{REQUEST_URI} !=/user
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([A-Za -z0-9_-]+)/?$ /user?name=$1 [PT,L]
Answer the question
In order to leave comments, you need to log in
What for? You will not directly access the css, js, img folders. Most likely you will access the files that are in them. Just add a rule:
RewriteCond %{REQUEST_FILENAME} !-f
In this case, if there is a direct access to the file, be it css or js, your RewriteRule will be ignored.
The solution may be simple, but a bit wrong. Let's say you added a new servlet, will you add the rules again?
do something like
RewriteCond %{REQUEST_URI} !=/app/*
Use /app/ in all servlets and you will be globally happy.
It's just that the tasks that I have to solve require that the servlet container be as close to the root / pulled as possible. Initially, everything was as you suggest. The servlet container handled everything in the /app/ context. In this case, even the RewriteCond construct with REQUEST_URI that you propose does not need to be written. Just enough:
RewriteRule ^/([A-Za-z0-9_-]+)$ /app/user?name=$1 [PT,L]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question