Forum Moderators: phranque

Message Too Old, No Replies

Mapping a domain to a subdirectory & vice-versa: Recursion problems

Using mod_rewrite to map domainB to a subfolder on domainA

         

firewing1

7:52 am on Mar 1, 2009 (gmt 0)

10+ Year Member



Hello,

I've found a few solutions to map a domain to a subdirectory (domainB is mapped to domainA/subfolder) on the Web and on this forum, and it's been working great except for two minor bugs I'd like to solve:
* Accessing domainB/subfolder/ still works
* Accessing domainB/anyfolder maps to domainB/subfolder/anyfolder, however domainB/anyfolder/ (with a slash at the end) works fine.

My filesystem is laid out as follows:

/
¦- .htaccess
¦- blog/ -(symlink)-> drupal/
¦- drupal/
¦--- <drupal installation here>
¦- web1/ -(symlink)-> drupal/
¦- web2/ -(symlink)-> drupal/

I've setup the required folders with drupal, so all is working there. Below is my .htaccess file:


Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#
# If we're not domainB and accessing /blog, redirect accordingly
#
RewriteCond %{HTTP_HOST} ^!domainB
RewriteCond %{REQUEST_URI} ^/blog/
RewriteRule ^blog/(.*)$ http://domainB.com/$1 [L,R=301]
#
# /X is really /blog/X on the filesystem
#
RewriteCond %{HTTP_HOST} ^domainB
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ /blog/$1 [L]
</IfModule>

I know that the first bug is caused by this part of my second rule:

RewriteCond %{REQUEST_URI} !^/blog/

But I don't know work around it. I've tried redirecting for /blog/blog-->/ which doesn't work, and testing for /blog--/ results in an infinite loop because of the first rule. I have no clue about what's causing the second bug though.

Thanks in advance for any help!

firewing1

8:00 am on Mar 1, 2009 (gmt 0)

10+ Year Member



Interestingly enough, this rule below solves both bugs, but only if I don't enable rewrites in the /drupal folder:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /blog.*\ HTTP/
RewriteRule ^blog/(.*)$ http://127.0.0.1/$1 [R=301,L]

If I understand correctly, %{THE_REQUEST} has to be used because %{REQUEST_URI} is changed by the previous rewrite rules, which is why I couldn't test for either /blog/blog (as this became /blog) or /blog (as this was every URL).

This rule works perfectly, but what is odd is that if I rename htaccess.txt to .htaccess in /drupal, it stops working and /downloads for example expands to /blog/downloads and domainB/blog/ will not shorten to domainB. If I set "RewriteEngine off" in /drupal/.htaccess, then it starts working well again.

jdMorgan

3:13 pm on Mar 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Likely an interaction between the .htaccess code in /drupal, you main .htaccess code, and the symlinks...

JIm

firewing1

5:33 pm on Mar 1, 2009 (gmt 0)

10+ Year Member



How would I go about solving it? At the moment, putting the %{THE_REQUEST} check into each subdirectory that contains a Drupal or Joomla installation works (at the moment, that's only two additional .htaccess files so it's not bad) but nonetheless if there's a cleaner way to solve this that would be better.

Edit: I see a similar problem with the "site1" and "site2" folders. I have a simple rule that redirects all non-www domains to their www ones, however typing "domainA.com/web1" gives me the Drupal installation page instead of adding the www first, so that drupal recognizes the site.