mod_rewrite: checking id file exists not working

Hi folks,

I’m trying using the following rule without success and nobody could help me so far:

RewriteCond %{DOCUMENT_ROOT}/apps/%{HTTP_HOST}/$1 -f
RewriteCond %{DOCUMENT_ROOT}/apps/%{HTTP_HOST}/$1 -d
RewriteRule ^(.*)$ /apps/%{HTTP_HOST}/$1 [L]

My folder structure is: /apps/[DOMAIN.COM]/ that’s why i need %{HTTP_HOST} there.
Example file: domain.com/apps/domain.com/file.jpg
Want to reach this as: domain.com/file.jpg but it seems like it’s jumping over the rule but the file exists…

wc,

I understand your code but believe that Domain.com’s DocumentRoot should be what you’ve labeled %{DocumentRoot}/apps/%{HTTP_HOST} so all your code should be skipped (not matched). If you’ve created your domain.com correctly, it should be to the (physical address of the) %{DocumentRoot}/apps/%{HTTP_HOST} directory.

But that’s only the first problem. These RewriteCond tests are ANDed so, to match both, the request must be to both a file and a directory!

Finally, the :kaioken: EVERYTHING :kaioken: atom you’ve used in the RewriteRule will match everything (and NOTHING) and will cause the block to loop.

You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too.

Regards,

DK

Nope, %{DocumentRoot} is /
There is my index.php too. I’ve did my framework to be able to handle multiple sites/domains in one instace. The /apps/%{HTTP_HOST} only holds controllers, views, and assets. Now my URL are look like this for example a CSS file:
http://domain.com/apps/domain.com/assets/style.css but i want to make like this http://domain.com/assets/style.css
I’m can’t write rules for css/subfolders/images/etc. cuz i’m giving my clients/project members access the /apps/domain.com folder only where they can do anything they want. That’s why i want to have a universal code for these things.

wc,

I believe you’ve defined an impossible problem because:

  1. The server’s root is the httpd’s root, too? :eek2:
  2. You want to use domain.com for your .htaccess. Just how many "domain.com"s do you have? Is there a domain.com for every {HTTP_HOST} (there must be so your single use - across many clients - is impossible). Are you aware that domain.com IS the {HTTP_HOST}?
  3. You only want to give access to apps/%{HTTP_HOST} to your {HTTP_HOST} client. Does this really mean that they don’t have access to domain.com’s root?
  4. Apparently, assuming the above to be incorrect, you’ve apparently not tested the changes to your mod_rewrite code as recommended above - I’m not sure how to get out of the loop you’ve defined in the RewriteRule, though.

Think out of the box! Develop offline in a test server then upload the test to a subdirectory of the actual server for the client to check (assuming they can’t view your test server in action). With a test server, you can create all the virtual host domains you need (I do this and either drop the TLD or use an acronym for a domain) making testing as close as possible to the real server.

Regards,

DK

Thanks for the effort, but i already did it.

RewriteCond %{DOCUMENT_ROOT}/apps/%{HTTP_HOST}/$1 -f
RewriteRule ^(.*)$ /apps/%{HTTP_HOST}/$1 [L]

RewriteCond %{DOCUMENT_ROOT}/apps/%{HTTP_HOST}/$1 -d
RewriteRule ^(.*)$ /apps/%{HTTP_HOST}/$1 [L]

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

Yeah, i could use [OR] statement in one block instead of two for the first 2 but it was throwing 500, dont know why. But this solution is working just great.

No, %{DOCUMENT_ROOT} is not my server root, that was a misunderstanding by my fault. But it’s the root for my PHP Framework.

apps
domain.com
domain2.com
domain3.com
system
index.php

That’s why i was needed that rule, so i can server directly from those subfolders just like in a normal virtual host :slight_smile:

wc,

Ah, a specificity problem! Isn’t that always the way?

Okay, I’m glad that you’ve gotten it working for youself. I still don’t follow the use of %{HTTP_HOST} when domain.com is always your {HTTP_HOST} (and you’re looking to match domain.com, domain2.com and domain3.com. I fear that you’ll still have problems with this unless you specify your values for each place where you’re using %{HTTP_HOST}.

Hmmm, have you defined all three domains with the same ServerPath? If that’s the case, you may have found a way to beat the system!

As for your 500 errors, I’d guess that you used the [OR] flag after both RewriteCond statements (the second one is the error).

By definition, {DOCUMENT_ROOT} is the physical path to the DocumentRoot of a domain, in this case, your domain.com.

Back to your original question, though: I don’t believe that you can hide the URI … but the new information may have changed that - especially if the three domains are actually colocated - and you would do it exactly the way that you have the code now.

:tup:

Regards,

DK