Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want mod_rewrite to do this:

http://server/* -> redirect to http://server/app/*
http://server/app/* should not be redirected

http://server.domain/* -> redirect to http://server/app/*
http://server.domain/app* -> redirect to http://server/app/*

It has to work with mod_jk!

Edit: this is the final solution

` force use of host 'server'
RewriteCond %{HTTP_HOST} !^server$
RewriteRule ^(.*)$ server$1 [R,NE,L]

` prepend /app to URL if missing
RewriteCond %{request_uri} !^/app.*?
RewriteRule ^(.+?)$ app/$1 [R,NE,L]

Thanks to you, fahadsadah and Insanity5902! I'm hesitant to flag either one of you as 'correct', as both have provided valuable input that made up the final solution.

share|improve this question

Use the following config directives (you probably want to add them to your .htaccess file):

RewriteEngine on
RewriteCond %{request_uri} !^/prepended
RewriteRule ^(.+?)$ prepended/$1
share|improve this answer
I tried combining the two approaches: RewriteCond %{HTTP_HOST} !^server$ RewriteRule ^(.*)$ server/$1 [R,L] RewriteCond %{request_uri}!^/app RewriteRule ^(.+?)$ app/$1 [R,L] That gives me: RewriteCond: bad argument line '%{request_uri}!^/app' – user27451 Jan 16 '10 at 17:10
Try RewriteCond %{request_uri}!^/prepended/.+? – fahadsadah Jan 16 '10 at 17:28
A space was missing, RewriteCond %{request_uri} !^/prepended/.+? seems to work. – user27451 Jan 16 '10 at 18:47
Edited the answer to contain the correct directives. – fahadsadah Jan 17 '10 at 8:31

This should work better

RewriteEngine On
RewriteCond %{HTTP_HOST} !^server$ [NC]
RewriteRule ^(.*)$ http://server/$1

RewriteRule ^$ /app

This is off the top of my head, but what should be happening. Request comes in, if it isn't using server for the host name, then it will redirect to http://server keeping the rest of the URI field intact. Then it will reprocess the rules again, skip the first one (since RewriteCond is false) and then process the RewriteRule. Which is saying, if the URI is empty send it to /app. If the URI is anything other then empty, it will use what is already there.

So on top of what you already have, it should also work as

http://server/foo -> http://server/foo
http://server.domain/foo -> http://server/foo

Let me know how that works, In my head it seems to work fine :)

share|improve this answer
Ok I'll try, is mod_jk a problem? – user27451 Jan 16 '10 at 16:53
"If the URI is anything other then empty, it will use what is already there." Can I rewrite the URL so that is /app is missing, it will prepend /app to the rest of the URL? Thanks so far! – user27451 Jan 16 '10 at 16:56
That is what the second rewrite rule does. The ^ tells it to look for the start of the URI and the $ looks for the end. So since it is empty it will forward it to /app – Ryan Gibbons Jan 16 '10 at 20:10
as for as mod_jk, I don't think it will be a problem , but not for certain as I've never used it. – Ryan Gibbons Jan 16 '10 at 20:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.