Forum Moderators: phranque

Message Too Old, No Replies

Mapping Domain to sub-folder conflicting with other directives

         

ambaxter

12:13 am on Feb 22, 2011 (gmt 0)

10+ Year Member



Hello all,

Per this thread [webmasterworld.com...]

I'm using JD's awesome code for mapping a domain to a specific subfolder and it works incredibly.

RewriteCond $1 !^folder2/
RewriteCond %{HTTP_HOST} ^www\.domain2\.com
RewriteRule (.*)/folder2/$1 [L]



However, the CMS I'm using includes a friendly URL script in the HTACCESS that seems to be conflicting with the above code.

When I try switching which one comes first, the first script wins out, thus nullifying the effect of the secondary script.

Here is the code that I'm juggling with this

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


Does anyone have an idea of how to make these two play well together?

g1smd

1:49 am on Feb 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



With the limited amount of information in the question, guesswork:

RewriteCond $1 !^folder2/
RewriteCond %{HTTP_HOST} ^www\.domain2\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /folder2/index.php?q=$1 [L,QSA]


Be sure this is what you want. In particular, you should add another RewriteCond ahead of the others that "filters out" requests for images, stylesheets and scripts. Since these are never going to be rewritten to the index.php file, there is no need to do the "-f" and "-d" exists checks for those requests. Those requests are very very slow and inefficient, requiring two physical disk reads per requested resource. For a typical page that could include several stylesheets and multiple images.

ambaxter

2:15 am on Feb 22, 2011 (gmt 0)

10+ Year Member



Thanks for this info. I tried that combination earlier (minus the -d and -f flags) and it didn't work.

Looks like I'm stuck with an either/or situation here.

g1smd

8:55 am on Feb 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Describe "didn't work" and explain what actually happened and how that differed from what you wanted.

ambaxter

3:33 pm on Feb 22, 2011 (gmt 0)

10+ Year Member



with the code that you present, the issue is the same as the conflict I get when I have them separate.

When the friendly URL code is first, the mapping script (which comes later) breaks and does not treat the domain as top-level like it's supposed to. All files have to be accessed using the full path to the folder in which they reside. (domain2.com/domainfolder/index.html)

When I put the domain mapping script ahead of the friendly url script, all of the css and graphics are visible and everything behaves as expected: I can access files with domain2.com/file.html. But then all of the friendly urls return 404s because it breaks the script (which comes after the mapping script in this scenario).

Something about the two scripts is negating crucial elements in the other. I've spent hours on this.

g1smd

5:46 pm on Feb 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is the .htaccess file located in the root or in folder2?

ambaxter

5:50 pm on Feb 22, 2011 (gmt 0)

10+ Year Member



it's in the root folder. the second folder has no htaccess

g1smd

5:57 pm on Feb 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You cannot internally rewrite the same request twice, so there cannot be two scripts. Both operations have to be done in one hit. Are you sure there is only one RewriteRule in the entire code?

ambaxter

6:07 pm on Feb 22, 2011 (gmt 0)

10+ Year Member



When I use

RewriteCond $1 !^folder2/
RewriteCond %{HTTP_HOST} ^www\.domain2\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /folder2/index.php?q=$1 [L,QSA]


The rewritten URLs do work. But it doesn't treat the domain as if it's top-level.

Files still only work by accessing them from their full urls (domain2.com/foldername/file.html).

jdMorgan

6:31 pm on Mar 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need two rules, one for the case where the requested domain2 URL-path resolves to an existing file or directory, and one for the case where it does not. The 'exists' RewriteConds could not have possibly worked because they were looking in the wrong directory. Try 'constructing' the proper (relocated) filepath like this:

# If URL-path requested in domain2 does not resolve to existing file or
# directory in /folder2, then rewrite the request to the /index.php script
# in folder2, passing the requested URL-path as a query string.
RewriteCond %{HTTP_HOST} ^www\.domain2\.com
RewriteCond $1 !^folder2/
RewriteCond %{DOUMENT_ROOT}/folder2/$1 !-f
RewriteCond %{DOUMENT_ROOT}/folder2/$1 !-d
RewriteRule ^(.*)$ /folder2/index.php?q=$1 [QSA,L]
#
# Else prepend /folder2/ directory-path to remaining domain2 requests unless already done
RewriteCond %{HTTP_HOST} ^www\.domain2\.com
RewriteCond $1 !^folder2/
RewriteRule ^(.*)$ /folder2/$1 [L]

Depending on your server configuration, you may have to tweak that filepath construct in the first rule's RewriteConds: If you get an error on the first attempt, look at the failed filepath given in the server error log, and adjust the RewriteCond filepaths accordingly.

Jim

ambaxter

8:05 pm on Mar 4, 2011 (gmt 0)

10+ Year Member



Jim,

This is really great information. It worked just as you described.

Thanks a thousand times! I have been working on this for quite some time.

g1smd

8:50 pm on Mar 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks Jim, I overlooked the additional path information required.