How to make Apache route all requests except some directories ?

Hi

Some context might help:

  • You have an Apache running environment, on which you are not root (therefore no access to the global Apache config). Typically the kind of hosted shared environment. You have full control on the files stored in your www/ directory.
  • You want to root all requests by default to a given URL (might be a directory, another port…).
  • But you also want some directories like www/realdir to be visible in a flat way

So first step, you define a nice www/.htaccess file in your directory to redirect everything by default:

RewriteEngine on
# Redirect every request to the port 12004, hosting a Ruby on Rails server
RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteRule ^(.*)$ "http\:\/\/127\.0\.0\.1\:12004\/$1" [P,L]

Then all your http://mysite.com/my/path/ URL will be routed to your alternate location (a nice Ruby on Rails server running on port 12004 for this example).

But then you want to get your www/realdir/index.html file to be accessible using the normal http://mysite.com/realdir URL. By default, Apache will also route this URL to your alternate location. And you want Apache to do it, not your alternate location.

The trick is to declare a simple .htaccess file in your directory, declaring the Rewrite engine.

RewriteEngine on

And that’s it, you can use this way on any directory you want !

Muriel

About Muriel Salvan

I am a freelance project manager and polyglot developer, expert in Ruby and Rails. I created X-Aeon Solutions and rivierarb Ruby meetups. I also give trainings and conferences on technical topics. My core development principles: Plugins-oriented architectures, simple components, Open Source power, clever automation, constant technology watch, quality and optimized code. My experience includes big and small companies. I embrace agile methodologies and test driven development, without giving up on planning and risks containment methods as well. I love Open Source and became a big advocate.
Howto, Web development , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *