Skip to content

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

Leave a Reply

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