views:

31

answers:

2

Hi!

I am working on a very dynamic system where i have two identical htaccess files in / and in /somepath. The reason for this that the domain could be pointed into /somepath, but i never know if it is.

When it is pointed to /somepath there are no problems, but when its not it seems like when i request /somepath/page/foo/bar the htaccess file in /somepath overrides the one in /. In the latter case i dont want the /somepath/.htaccess to run at all, or at least disregard the mod_rewrite in it.

One solution would be if i could check if the latter htaccess is not located in the document root. Is this possible? How can i compare the htaccess path to document root from within the htacces file?

Both htaccess files look like this:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule .* uri_handler.php [L]
</IfModule>

Does anyone know whats going on here? Thanks!

A: 

I don't think this is possible. But this sounds like a construction error either way. It would be much better to incorporate the two .htaccess files into one. (Or is the second one in /somepath even necessary? Why?)

Under which circumstances does the request's domain point to /somepath? Is it a different domain you could use in a RewriteCond of its own?

Pekka
The pointing of the domain into /somepath is a user choice which i can not control. And thats when its needed in /somepath.
komakino
@komakino - When the domain is pointed at `/somepath`, does `/somepath` become the `DocumentRoot`? Also, does `uri_handler.php` care what directory it's in, or regardless of where the domain points, can it do its job no matter where it's located?
Tim Stone
@komakino so it doesn't work to simply remove the htaccess in /somepath? (Not sure right now where it will look for the relatively specified `uri_handler.php` in that case.)
Pekka
@Tim - Yes, it becomes the document root. The uri_handler could do its job either way but it brings other logical problems for me that are hard to explain.@Pekka - I cant remove it since i do not know if it is needed. Oh, there are uri_handlers in both directories.
komakino
@komakino you *can* remove it: `.htaccess` files apply to all child directories. A request to `/somepath` *will* be caught by the htaccess file in the parent directory. The only question is whether the RewriteRule will be parsed correctly (i.e. whether the url_handler.php reference points to the correct file)
Pekka
But if the domain is pointed to the /somepath, that becomes the document root and the .htaccess file in / is not located on the website anymore.If its not pointed and requesting /somepath, the rewrite rules in the /.htaccess are totally ignored.
komakino
@komakino - I see...Confusing. :P Is it alright if one `.htaccess` file is different than the other (I don't know why it would be an issue, but I figured I'd ask)?
Tim Stone
Yeah i know ;) Thats a valid question but yes, the htaccess files can be different.
komakino
A: 

Barring any attempts at simplifying your setup, I think that the easiest approach here is to just make sure that the .htaccess file in /somepath doesn't handle requests to /somepath when the document root is /.

Assuming we're all on the same page about the document root, we can accomplish this by modifying /somepath/.htaccess:

Edit: The easier way is to just have it always request the uri_handler.php that lives at the root:

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_FILENAME} \.php$
  RewriteRule .* /uri_handler.php [L]
</IfModule>
Tim Stone
I already tried that, but the rewrite logic in /.htaccess is still ignored. So i just get a regular 404.
komakino
I mean i get a regular 404 when requesting a fake uri like /somepath/foo/bar. When requesting /nonexistantpath/foo/bar the logic in /.htaccess works fine.
komakino
Hmm, you're right. I'm going to have to go back and double-check the source code on this one, since it's definitely not what I had anticipated. Interesting. I'll edit with a fix, though.
Tim Stone
@komakino - And actually...I think it can be done as easily as adding the forward slash, regardless, whoops.
Tim Stone
First of all, i get a 500 with log entry "Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace."Secondly - i know this is a pain in the a** - i also need to support the whole system to be placed in a subdir. Then the upper uri_hanlder would not be in the doc root either ;)
komakino
And at this point the uri_handler file only contains die($_SERVER['SCRIPT_NAME']);
komakino
@komakino - Do you have the ability to change the main server configuration so that you can set `RewriteLog`? I don't get that behaviour on my test server, so it would be helpful to see the request chain from your side. ...Oh. Well...I might need to wake up some more and drink some coffee before I try to work that out, but I imagine there's a solution to be had somewhere.
Tim Stone
http://pastebin.com/JHKTg5SRGot to go to lunch, back in an hour. Thanks so much for your time.
komakino
Well, RewriteRule !^uri_handler.php /uri_handler.php [L] fixed the 500. So thats great. Now the only problem is if the system is in a subdir, but i guess i'll have to fix that some other way. Thanks!
komakino
@komakino - Ah, of course, because `RewriteCond %{REQUEST_FILENAME} \.php$` will match (I thought it was `!\.php$` for some reason). My `mod_rewrite` caught that I was rewriting the URL to itself and stopped the redirect loop, but yours must not have (I forget when that was added). If I think of a solution to the subdir issue, I'll update my answer, but otherwise good luck!
Tim Stone
Thank you so much!
komakino