apache mod_rewrite

Posted on

apache mod_rewrite – Problems with loading a website are often blamed on the Internet connection, but even the most perfectly set up network cannot help if there is no service to reply at your destination. One of the most popular HTTP servers used for this task is Apache2. Much of Apache’s popularity can be attributed to its easy installation and use, but never the less it is possible to run into problems with even the easiest of the software. If you’ve encountered an issue loading your web page, follow these simple troubleshooting methods outlined in this guide to attempt to get your web server back up and working again. Below are some tips in manage your apache2 server when you find problem about apache-2.2, mod-rewrite, , , .

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.

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

RewriteEngine on
RewriteCond %{request_uri} !^/prepended
RewriteRule ^(.+?)$ prepended/$1

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 🙂

Leave a Reply

Your email address will not be published.