K
K
KhanTengri2011-06-12 02:40:55
Apache HTTP Server
KhanTengri, 2011-06-12 02:40:55

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

3 answer(s)
A
ADOLF88HITLER, 2011-06-12
@ADOLF88HITLER

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.

M
max_rip, 2011-06-12
@max_rip

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.

K
KhanTengri, 2011-06-12
@KhanTengri

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]

And all requests for virtual directories will simply not touch the one handled by the servlet container. However, in this case, the index page cannot be fundamentally dynamic ... It will either be html (http://example.com/index.html) or it will also need to be rewritten to a jsp or servlet in the /app/ directory, which not right.
In my example, the index page can be dynamic (http://example.com/index.jsp) without rewrites
Regarding the fact that each servlet will have to be written manually - there are not so many of them and they do not change / add so often .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question