Forum Moderators: phranque

Message Too Old, No Replies

Redirecting to a new root directory

Emergency problem... please help!

         

neophyte

2:09 pm on Apr 15, 2008 (gmt 0)

10+ Year Member



Hello All -

Boy, am I in trouble! Here's the scenario:

Got a client site where all site-related directories (and index.php) were all simply in the root. Then I was told to build a back-end admin area, which I did. So then... I decided... that I'd put all the front-end related directories (and index.php) in a NEW directory in the root named ".public". And then, because I thought this was such a slick idea, I'd put all the back-end directories (and the index.php for the back end) in another NEW directory called ".private". Now everything is neat and tidy.

So, I wondered how I would redirect the www.domain.com to fetch the index.php page inside ".public"... well, I'll just put "DirectoryIndex .public/index.php" in the htaccess file. I did that, went to the base url and it worked. "Cool - I'm a genius." Then, I decided to test the live links and clicked on one: "Not Found. The requested URL /index.php was not found on this server."

"Oh no! I'm an idiot!"

Ah, well, yes, it's looking for index.php one level up... but what to do? So I began a furious Google search on "new root directories, htaccess" ect and came up with a bunch of stuff - which I tried but without success (and a great deal of hair-loss), including one suggestion on some forum which yielded the dreaded "Internal Server Error" message. "Oh no! I'm DEAD!"

I know (well, I believe and REALLY HOPE) there's a simple solution to this quandary I've gotten myself into. Please somebody tell me what I can do to get ".public" to act like an ordinary root via htaccess... please... Gobs of good karma no doubt await someone who can show me how to fix this.

The simplest solution is - at this point - the best for me - my nerves are shattered.

HUGE appreciation to all in advance.

Neophyte

jdMorgan

4:26 pm on Apr 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The solution (if there is one) depends on the answer to this critical question: How is the server to decide, while looking only at the incoming URL request, whether the request should be directed to /.public or to /.private? (I'm talking about the address in the browser address bar; Looking only at that, how can you tell which directory the request should 'land' in?)

If there is something definitive (detectable using regular-expressions pattern-matching) about these two groups of URLs that you can base this decision on, then there is a solution. If not, then your alternatives are:

1) Back out the subdirectory changes.
2) Change the URLs (links on your pages) to make them definitive.
3) List all URLs in one group or the other, rewrite based on that list, and maintain/update this list forever.

Jim

neophyte

5:54 pm on Apr 15, 2008 (gmt 0)

10+ Year Member



Jim -

Thanks for your reply. Regarding your point >>whether the request should be directed to /.public or to /.private?<< this is (I think) an easy answer: ALL "general public" requests should land within - and stay with - the ".public" directory ONLY.

The ".private" directory and containing files is - on the other hand - only to be accessed "manually" by an admin typing in the full URL: www.domain.com/.private/index.php. I've tried this and can navigate all pages of the .private directory just fine (i.e. every link in the back end that I click DOES show up as www.domain.com/.private/index.php?cmd=s1,p3 (and so on).

Interestingly, I get the same (and desired) result if I manually type the full url to the ".public" directory (www.domain.com/.public/index.php)... if I do that, the site also navigates perfectly (I STAY within the .public directory) and the www.domain.com/.public/... stays in the address bar.

Conversely, however, when you just type in www.domain.com, the user is INITIALLY directed to to the index file within ".public" (via DirectoryIndex .public/index.php that I already have in my htaccess) but once a link within .pubic is clicked, requests revert back to the REAL root of the site, rather than "landing" back inside the .public sub-directory.

I didn't think that I was playing with fire when I decided to go the sub-directory route, and I'm sure it is more than obvious to those that read these posts that I'm not at all versed in servers and all the things that htaccess can offer. That being said, I'd hate to have to go with option 1 or option 3 as noted in your reply. I guess I could deal with option 2 if you're saying that all links would have to be re-coded to ".public/index.php?cmd=whatever" where they're now just "index.php blah, blah..."

But, but, but... given that the desire is to "simply" send a user into the .public sub directory and KEEP them their during their navigational stay, isn't there some htaccess directive that would "hardwire" a user into ONE specific sub directory?

Sorry for the long, long re-explanation but I think that my situation may be clearer now.

Thanks again for your (and everyone else's) time who have waded through this.

Neophyte

jdMorgan

6:25 pm on Apr 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it is true that all requests for /.private will be made explicitly -- that is, with "/.private" in the URL itself and in any links within that area, then this meets the requirement I posted as #2 above. The code can be implemented with the logical rules: "If the client-requested URL-path starts with neither '/.private' nor '/.public' then rewrite it to '/.private', otherwise leave it alone."

Something like:


RewriteCond $1 !^\.(public¦private)/
RewriteRule (.*) /.public/$1 [L]

You may also want to redirect direct client requests for "/.public" back to the main-directory URL in order to prevent accidental duplicate-content issues:

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

As stated, this code snippet will redirect only if the client directly requests a URL with /.public in it. It will have no effect on URLs rewritten by the other rule above. It uses the RewriteCond to do this, because without that RewriteCond, the two rules would interact, and you'd get an "infinite" rewrite/redirect loop.

As shown, this code is intended for use in the top-level .htaccess file, and will need to be modified for use in httpd.conf or conf.d.

Replace all broken pipe "¦" characters with solid pipe charaqcters before use; Posting on this forum modifies the pipe characters.

Jim

g1smd

12:42 am on Apr 17, 2008 (gmt 0)

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



jd beat me to it.

That's what i would do too, but I would also fix it so that I never see "index.php" appearing in any URL. I would 301 redirect those back to trailing-slash-ending folder names.

The ORDER of such a redirect is crucial to the correct operation of the whole system. There would need to be several rules to correctly complete it, as it would also be vital to NOT create any sort of redirection chain.

- redirect /public/ or /public/folder/ "index" URLs to / or /folder/ without index filename included
- redirect all other /public/ and /public/folder/ files to / or /folder/
- redirect /private/ and /private/folder/ index files to /private/ or /private/folder/ without index filename included
- rewrite non-private URLs to internal filepath "one-level-down".

That's a slightly more complete and robust solution and example code appears in some earlier threads.