views:

60

answers:

1

I want to redirect all requests to a certain path on my server (/app) to a subdirectory at /app/app_site. Following rewrite rules do the job for requests like 'http://localhost/app/somepage.htm':

RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app(.*)     /app/app_site$1 [L,PT]

This results in the correct page, while preserving the URL. Also, 'http://localhost/app/' will fetch the index page at /app/app_site/index.html, while preserving the URL 'http://localhost/app/'.

However, when I enter 'http://localhost/app', following things happen:

  • the correct page is fetched, at /app/app_site/index.html
  • yet, the URL is redirected to 'http://localhost/app/app_site/'

I'm nearly there, but would like to preserve the URL in all cases (also those without trailing slash). Anyone have a clue how to do this? Thanks!

+1  A: 

This is the expected behaviour with DirectorySlash enabled, because you've rewritten to a directory that lacks a trailing slash, and mod_dir performs this cleanup after you've rewritten the URL with mod_rewrite.

The easiest solution is to rewrite the URL so that it always at least matches the slash-terminated directory path, like so:

RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app/?(.*)$     /app/app_site/$1 [L,PT]

This prevents mod_dir from having to add the trailing slash, and therefore avoids the external redirection to /app/app_site/ you're experiencing now.

Tim Stone
Perfect, thanks a million! This works like a charm for this example. However, there's still a problem with my real app. I simplified the example, but actually, /app is a Cocoon app served via Tomcat through mod_jk. Now, whenever I activate the mod_jk directive, "http://localhost/app" and "http://localhost/app/" are rewritten to "http://localhost/app/app_site/" (all URLs with further paths are preserved, though). Do you happen to know what causes this interference and how to solve it?
rvdb
They're rewritten in the sense that the URL is changed in the user's browser? How do you have your `mod_jk` handler defined?
Tim Stone
Hi Tim, I've investigated this furher and was put on the right track by your answer. I have no special options configured for the mod_jk handler; just one rule: /app|/*=ajp13.I realised that my cocoon app's sitemap.xmap specifies a redirection, too, for serving a default homepage when the empty path ('/app/') is requested: <map:match pattern=""> <map:redirect-to uri="appindex.xml"/> </map:match>This passes on the request to another cocoon pipeline. As it happens, this is equivalent to an external redirect, causing the URL ('/app/') to be rewritten as '/app/appindex.xml'. [...]
rvdb
[...] Of course, when served via mod_jk, this redirection happens on the URL that had been rewritten by mod_rewrite, causing "http://localhost/app/" to be rewritten as "http://localhost/app/app_site/appindex.xml". I could solve this by specifying the cocoon redirect as an internal redirect, by prefixing the "cocoon:/" schema: <map:match pattern=""> <map:redirect-to uri="cocoon:/appindex.xml"/> </map:match>This works: it preserves the URL "http://localhost/app/", while serving /app/app_site/appindex.xml.So, thanks again! As soon as I have sufficient credits, I'll vote your answer up.
rvdb