Solved

.htaccess - change document root

Posted on 2008-10-17
7
10,641 Views
Last Modified: 2013-12-13
Hello,

I have the following directory structure:

<ROOT>
    - .htaccess
    * mydomain.com
        - index.php
        * images

What I need is that every single hit to http://www.mydomain.com/anything would be transparently translated to http://www.mydomain.com/mydomain.com/anything. Thus the address in browser would not change in any way ... Need just hidden folder rewrite.

Please note that if the structure would look like this:

<ROOT>
    - .htaccess
        * mydomain.com
            - .index.php
            * mydomain.com

Then any hit to www.mydomain.com/mydomain.com/anything would be actually translated to www.mydomain.com/mydomain.com/mydomain.com/anything and without changing the browser's URL IN ANY WAY. Just transparent translation.

I tried this code (doesn't work...):

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ /mydomain.com%{REQUEST_URI} [QSA,L]

or

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ mydomain.com/$1 [QSA,L]

or

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ /mydomain.com/$1 [QSA,L]

Nothing worked well... do you know how to transparently redirect the document root?

I'm using Apache + PHP + mod_rewrite (I don't have access to httpd.conf).

Thank you very much.
0
Comment
Question by:JiriNovotny
  • 4
  • 2
7 Comments
 
LVL 27

Expert Comment

by:caterham_www
Comment Utility
> (doesn't work...):

That means it doesn't work at all, a HTTP 404 is being returned? You may test the functionality of mod_rewrite with a simple rule which redirects every request to google:
RewriteEngine on

RewriteRule ^  http://www.google.com  [R,L]

Open in new window

0
 

Author Comment

by:JiriNovotny
Comment Utility
Well mod_rewrite works great, just the rewrite rules don't work as expected.

Eg:

When I enter www.mydomain.com/localdir it is redirected (in browser URL) to www.mydomain.com/mydomain.com/localdir. And it should NOT be redirected that page in browser's URL. Redirection should be transparent.
0
 
LVL 44

Expert Comment

by:scrathcyboy
Comment Utility
First things first -- the HTACCESS file is IN the web root folder, and is read from that ROOT folder.  So you cannot use the HTACCESS to redirect the ROOT folder, since it has to reside in the root folder.

What you are asking I think is to translate all pages in public_html of your root to -- public_html/folder2 of your website, is this correct?

Also you need to state the this is normal Linux hosting, as M$ IIS hosting is TOTALLY different.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:JiriNovotny
Comment Utility
Yes, .htaccess is in root folder and it can be in root folder, just the actual website data must be in subfolder.

Yes, I need to translate all pages/images/data in public_html to public_html/folder2.

And yes, this is normal Linux hosting, sorry for confusion.
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 total points
Comment Utility
> it is redirected (in browser URL)

But not through your rules above, none should cause a redirect (no R-flax, no scheme+fqdn).

I assume that there are no other rules which may interact with the one below.
RewriteEngine On

# fix trailing slashes since mod_dir will redirect to /mydomain.com/...

RewriteCond %{REQUEST_URI} !/$

RewriteRule !\.[a-z]{2,4}$ %{REQUEST_URI}/ [R=301,L]
 

RewriteCond %{ENV:REDIRECT_STATUS} =""

RewriteRule ^(.*)$ /mydomain.com/$1 [L]

Open in new window

0
 

Author Comment

by:JiriNovotny
Comment Utility
caterham_www:
Thanks, your solution is almost perfect. Just one last question:

I think the redirection was caused by mod_dir. Is it possible (and how) to disable mod_dir in .htaccess?
0
 

Author Comment

by:JiriNovotny
Comment Utility
OK, I've figured out and attached the solution. Thank you for helping me figuring this out.
Options +FollowSymLinks +MultiViews -Indexes

Options -Indexes
 

IndexIgnore */*
 

<IfModule mod_dir.c>
 

  DirectorySlash Off
 

</IfModule>
 

<IfModule mod_rewrite.c>

  RewriteEngine On

  RewriteBase /
 

  # The mydomain.com domain redirect

  RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]

  RewriteRule ^(.*)$ /mydomain.com/$1 [QSA,L]
 

</IfModule>

Open in new window

0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now