Forum Moderators: phranque

Message Too Old, No Replies

Redirecting from root to subdir.but only for specific situations

mod_rewrite redirection

         

charlener

3:10 pm on Sep 1, 2005 (gmt 0)

10+ Year Member



I've done some searching on the boards, which has gotten me most of the way where I want to go (perhaps not clean, but it works) - but for one issue.

I'm using a domain where the web root (a CMS system) is in a subdirectory, drupal. I want for users when they type in the url www.myurl.com that it goes to www.myurl.com/drupal to show them the information without showing the redirect, i.e., the address bar is still at www.domain.com. I've modified the following code from another post to do this:

# Rewrite index.htm OR index.html OR / to /anydir/index.htm
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(index\.php?)?$ /drupal/index.php [L]

# I thought this would do it, but it makes the server crap out with a 500 error
# RewriteCond %{REQUEST_URI}!^/survey/
RewriteRule ^(.*)$ /drupal/$1 [L]

And it works wonderfully. the problem is when I'm trying to access another subdirectory under the root, say, [domain.com...] I believe the above rules redirect this, also, to www.domain.com/drupal/survey, where I'm told the page doesn't exist (which it doesn't).

What would I need to do to make this work properly? Hope that explanation made sense...

jdMorgan

3:54 pm on Sep 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



charlener,

Welcome to WebmasterWorld!

The key is to use RewriteCond to make the rewrite (this is not an external redirect, it's an internal "silent" rewrite) conditional, like the line you've got commented-out:


# RewriteCond %{REQUEST_URI} !^/survey/

This line, if it were active, would prevent requests for any resource in the subdirectory "/survey" from being rewritten.

So, you'll need to generalize that so that only requests for pages in the root directory are rewritten to /drupal. You also need to do this to avoid rewriting requests for /drupal to /drupal/drupal, etc. which would cause an "infinite" loop.


# Rewrite requests for root directory resources to /drupal subdirectory
RewriteCond %{REQUEST_URI} !^/.+/
RewriteRule (.*) /drupal/$1 [L]

The RewriteCond allows the rewrite only if the requested URL does not contain additional characters and a slash after the initial slash. This prevents subdirectory requests (including those for /drupal) from being rewritten.

This may make more sense if you realize that once mod_rewrite rules are applied, the server re-preocesses all config directives in the new URL-path, so that rewrites and access restrictions that apply to that new path can be applied. This is needed to avoid user-created security holes. This makes mod_rewrite appear to act recursively. Because of this, you have to make sure the code is written to avoid rewriting /drupal to /drupal/drupal, etc. The single RewriteCond above solves both problems.

Jim

charlener

4:13 pm on Sep 1, 2005 (gmt 0)

10+ Year Member



Hmmm...that makes sense...here's the thing, though...it seems that when I use that, all my stylesheets and templates for the cms just rather disappear. Before, (the way that worked too well), I had changed the php config file for the cms to be the root, even though it wasn't, becase the rewrite would take care of that and use the subdirectory. I'm not sure if it's doing that anymore - it's pretty heavily nested, eg., themes are in drupal/themes/themename - and the same goes for the administrative backend.

What do you all think?

[edited by: jdMorgan at 5:22 pm (utc) on Sep. 1, 2005]
[edit reason] No URLs, please. See TOS. [/edit]

jdMorgan

5:29 pm on Sep 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you'll need to define precisely what it is that you want to do. And/or tweak the file organization and URL-paths so that this simple code works.

You asked how to avoid rewriting anything that is in a subdirectory, and so that's what the code I posted does. The nature of rewriting is that the definition of what is to be rewritten and/or what is not to be rewritten must needs be precise -- No wiggle-room whatsoever.

As it stands, the URLs for all resources accessed by your drupal pages need to "appear" to be in root, otherwise they will not be rewritten.

You may want to consider different ways of organizing this, such as a list of pages and directories that *do* need to be rewritten to /drupal, or alternately, a list of pages and directories that should not be rewritten. It is also possible to combine the two, as long as the resulting specification is complete and concise.

Jim